Page 1 of 1

simple select does not work - beginner question

Posted: Mon Aug 25, 2008 5:38 pm
by dengar
Hi everybody and thanks for your help!

This seems to be a very easy issue for someone experienced with PHP, but I am not (apparently)...

Ok, I have been sweating over this for quite some time now (maybe a week, on and off?):

I have a table where I plan to write session information: 'Session' is this tables name.
I have a couple of lines in there already, but since my site isn't finalised, I keep deleting them and only keep line 1.

This doesn't really matter, I guess, because all I want to query is the highest number in that table...
Not so difficult one should imagine:

Code: Select all

 
<?
   $result = mysql_query("select max(SessionID) from Session") or die(mysql_error());
   $rest = mysql_fetch_field($result, 0);
   echo " Top 1 SessionID is: ". $rest;
?>
 
But my problem is:
Catchable fatal error: Object of class stdClass could not be converted to string in {...(line "echo...")}
So, I donno, but I think PHP doesn't need to convert variables, isn't that correct? So it doesn't really matter that my string is actually an int.
I seem to be struggling quite a bit with the fetch-part. I can't fetch this into an array, no object, nothing. (<span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> me off)
If I comment the "echo" line out, it works fine, no error.

Is the $result empty? Is there a way to tell (other than fetching it)?

The query works fine in a query browser, so I think this statement should be legit? - It should give me some error if it wasn't, right?

Any other suggestions how I might be able to debug?

Thanks again in advance!

Best,

Chris

Re: simple select does not work - beginner question

Posted: Mon Aug 25, 2008 6:03 pm
by dajawu
mysql_fetch_field returns an object containing field information not the actual field itself. Use mysql_fetch_row, and access the variable with $rest[0].

Code: Select all

 
$result = mysql_query("select max(SessionID) from Session") or die(mysql_error());
$rest = mysql_fetch_row($result);
echo " Top 1 SessionID is: ". $rest[0];
 

Re: simple select does not work - beginner question

Posted: Mon Aug 25, 2008 7:48 pm
by dengar
Thanks dajawu,

I tried as you suggested, but one problem remains: it's not the right return...

Code: Select all

 
<?php
 
$result = mysql_query("select max(SessionID) from Session") or die(mysql_error());
$rest = mysql_fetch_field($result, 0);
$rest = (int) $rest;
 
echo " Top 1 SessionID is: ". $rest;
?>
 
This returns: 1

But the highest SessionID in the table is higher: 51 at the moment...

How come the query doesn't return the right result?

-Chris

Re: simple select does not work - beginner question

Posted: Mon Aug 25, 2008 7:57 pm
by califdon
Read dejawu's code again. You are using mysql_fetch_field(). You should be using mysql_fetch_row(), which returns an array containing the names and values of the fields returned by one row of the query.