0

I have a funny error while I'm trying to output a single variable assigned from database.

I'm querying a top id from database and assigning it to a variable. Here:

    $ID_Query = "SELECT DISTINCT MAX(pict_id) FROM picts;";
$temprID = mysql_query( $ID_Query, $Connection ) or die("ERROR: ".mysql_error());
$myID = mysql_fetch_row( $temprID );

But when I'm trying to pass it to the query or simply output $myID I'm getting the Array to string conversion error.

I tried to run the query in mysql and it returns a single value. Where is my mistake?

2 Answers 2

1

While the query returns a single value, fetch_row doesn't - it returns a row (who thought?) containing only one field

$result = mysql_fetch_row($temprID);
$myID = $result[0];

Also keep in mind that mysql_* functions are officially deprecated and hence should not be used in new code. You can use PDO or MySQLi instead. See this answer on SO for more information.

1
  • Thank you. Will try that. So silly not to think about it. Thank Commented Dec 6, 2013 at 1:15
1

Currently $myID hold a row result.

You need to access...

$myID[0]

... to get the field in that row.

You could also use mysql_result to get the value directly from $temprID:

$myID = mysql_result($temprID, 0);

Obligatory notice: you are using deprecated mysql_* methods and should switch to mysqli_* or PDO.

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.