Jump to content

Recommended Posts

I have a script that pushes ads into my table,

on that same page I want to have a link to it saying view.php?ID=.$mysqlID;

where $mysqlID is the ID number autokeyed for this entry.  What is the best way to retrive this ensuring I get this exact result.

I do have a field called Creator and since you can only create one ad at a time I figure the best way would be to select WHERE Creator='$creator' AND Creatordate = '$today', but then I need to figure out how to pick the greatest ad.  I'm sure there is logically ways with while functions, but i need a simple solution?

 

My Other Idea which is probably way easier would be how can I easily pull the last entry to the table (they have IDS that are Autokeyed)

and then I can easily get its ID number.  I could just say mysql select * $num rows. I'll try that unless someone gets me a better solution

to the last message, I do not know ID because it is generated from autokey in mysql table, but i'm trying this.  Unless I get bombarded I think it will work.:

$sql="SELECT * FROM $table";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$idnum=mysql_num_rows($result);
echo "Ad has been placed </br> <a href='view.php?ID=".$idnum."'>Click Here</a> To view your ad";

 

any one think this is a bad idea?  Auto key starts at 1 so.

I don't want to be considered a "bad" programmer so if you see an issue with my ideas than feel free to tell me what is considered compliance  to standards that aren't standard :) I try and work my css/html to xhtml compliance on w3 compliance tester and I feel my php should be industry accepted

This method you posted....

 

$sql="SELECT * FROM $table";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$idnum=mysql_num_rows($result);
echo "Ad has been placed </br> <a href='view.php?ID=".$idnum."'>Click Here</a> To view your ad";

 

Would be considered bad practice and unreliable. There is no guarantee the last record inserted will be at (what you consider) the end of the table.

 

My method will never fail to get the last inserted record.

unless some how the two insertions get stacked in the queue at the same time and the test for the last key happens after the second one gets done.  Probably will not happen, but there is that 1-10000000 chance it would.

i'm using:

$idnum=mysql_insert_id();
echo "Ad has been placed </br> <a href='view.php?ID=".$idnum."'>Click Here</a> To view your ad";

 

Relying on id's to order anything is frowned upon. That is not there intended purpose.

Thanks for the lesson in db theory.

 

Have you ever come across an instance where, with an autoincremented id, the highest value is not the last one added?

exactly, but if this was on a good size scale there is that potentially it could happen, but even then their data is across so many tables the tables wouldn't see each other and the odds shot up to 10^99 which we in the computer world call impossible

I think, and I could be wrong, that the mysql_insert_id function gets the id of the last inserted row by a connection, not from the table overall.  Since the server, both mysql and php, keep each connection seperate, even if a thousand other queries happen from other users visiting a page, it shouldn't affect the result of the function.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.