Forum Moderators: coopster

Message Too Old, No Replies

how to return the max id number from one of my sql tables

Tryin to figure out php with sql

         

Same_as_You

1:18 am on Oct 13, 2006 (gmt 0)

10+ Year Member



I've been building my website for the last couple months and still consider myself a newbie with php. OK, I am trying to figure out how to return the max id number from one of my sql tables through php. I believe that mysql_insert_id() will (correct me if I am wrong) give me the id number if an insert was made on that connection. But I also want to simply get the max id number at any given time.
So the code I tried using was:
SELECT MAX(PersonID) FROM `PersonTable`
which I plugged into my SQL database, and it came up with the correct number for me. It even gave me the php code:
$sql = 'SELECT MAX(PersonID) FROM `PersonTable`';
And then I added:
$result = mysql_query($sql);
echo $result;

But no worka. $result is an empty string. I tried:
$row = mysql_fetch_row($result); which also no works.
Anybody know what to do?

[edited by: jatar_k at 1:34 am (utc) on Oct. 13, 2006]
[edit reason] no urls thanks [/edit]

eelixduppy

1:54 am on Oct 13, 2006 (gmt 0)



Welcome to WebmasterWorld!


$row = [url=http://us3.php.net/manual/en/function.mysql-fetch-array.php]mysql_fetch_array[/url]($result);
[url=http://us3.php.net/manual/en/function.print-r.php]print_r[/url]($row);

Good luck!

Same_as_You

2:23 am on Oct 13, 2006 (gmt 0)

10+ Year Member



Thank you. I kinda just figured it out on my own, (using the print_r command) after wondering about it for about 3 weeks now: To get the resulting number, I need to use $row[0]. Thanks.

eelixduppy

2:27 am on Oct 13, 2006 (gmt 0)



Glad you got it!

>>after wondering about it for about 3 weeks now

Just wish you found this site earlier ;)