1

I've got a table in my SQL Database, and the table has two fields, one for an id, and the other for an integer. I want to grab the value in the integer field where the id is equal to a specific number I pass in. The id field is called "id" and the integer field is called "loglevel". This is the code I've got, but it doesn't give me the desired result.

$result = mysql_query("SELECT loglevel FROM Logs WHERE id='$number'");
echo "Pulled from SQL: " . $result;

The output for this is

Pulled from SQL: Resource id #2

Can you help me so the output is "2" if the value in the SQL table is 2?

2 Answers 2

4

You need to fetch your result using mysql_fetch_assoc(), or mysql_fetch_array() (or others). $result as returned by mysql_query(), is a result resource, not an actual rowset:

$result = mysql_query("SELECT loglevel FROM Logs WHERE id='$number'");

// If the query completed without errors, fetch a result
if ($result) {
  $row = mysql_fetch_assoc($result);
  echo $row['loglevel'];
}
// Otherwise display the error
else echo "An error occurred: " . mysql_error();

When you are expecting multiple rows returned rather than just one, fetch them inside a while loop. There are many examples of this in the mysql_fetch_assoc() documentation .

Sign up to request clarification or add additional context in comments.

Comments

0

$result = mysql_query("SELECT loglevel FROM Logs WHERE id='$number'");

here $result is a record set/ mysql resource not php array. you need to use mysql_fetch_assoc(), or mysql_fetch_array() to access $result.

if ($result) {
  while( $row = mysql_fetch_assoc($result)){
    $my_assoc[] = $row;
  }
}
var_dump($my_assoc);

this will print all data from the query.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.