simple select does not work - beginner question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dengar
Forum Newbie
Posts: 2
Joined: Sat Aug 16, 2008 10:50 am

simple select does not work - beginner question

Post 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
dajawu
Forum Commoner
Posts: 59
Joined: Fri May 23, 2008 10:16 am

Re: simple select does not work - beginner question

Post 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];
 
dengar
Forum Newbie
Posts: 2
Joined: Sat Aug 16, 2008 10:50 am

Re: simple select does not work - beginner question

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: simple select does not work - beginner question

Post 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.
Post Reply