Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Now that I feel like a complete idiot

Status
Not open for further replies.

TonyRosen

Programmer
Jul 28, 2003
108
US
If my database has 1 row, everything is flawless. If it has more than 1 row, I get errors.

Basically, I want to open my database, select the LAST entered row (highest "id"), and use that id ...

What I have:
---------------------------------------
$query = "SELECT * FROM my_register ORDER BY id DESC";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$lastid = mysql_result($result,"id");
}
mysql_free_result($result);

---------------------------------------

What am I doing wrong?
 
Can you post some errors ?
Just a thing if you get an error with more than 1 row are you hitting a duplicates not allowed thing ?
 
I can't decipher your methods, by why don't you try:

select id from my_register order by id descending limit 1;

or, perhaps:

select max(id) from my_register;

 
You shouldn't be invoking mysql_result at all. You've already fetched the data with mysql_fetch_assoc().

And if you only want one record from the table, then only fetch one record:

$query = "SELECT * FROM my_register ORDER BY id DESC LIMIT 1";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$row = mysql_fetch_assoc($result));
mysql_free_result($result);
$lastid = $row['id'];




Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top