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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
meks
  • 777
  • 3
  • 12
  • 23

2 Answers2

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.

jszobody
  • 28,495
  • 6
  • 61
  • 72
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.

Community
  • 1
  • 1
kero
  • 10,647
  • 5
  • 41
  • 51