Jump to content

Getting the ID from a MySql table?


dominod

Recommended Posts

Hi

 

$eid2 = mysql_query("SELECT id FROM engines WHERE keyword = '%$tricker_engine%' LIMIT 1") or die(mysql_error());
$row = mysql_fetch_assoc($eid2);
$eid = $row['id'];

 

I want $eid to be the ID of the row where keyword = '%$tricker_engine%'.

 

What is wrong with my code above?

Link to comment
Share on other sites

If you are expecting exactly one result, such as you would be when selecting a primary key or unique index field, don't use a WHERE . . . LIKE query. By doing so, and combining it with a LIMIT 1, there's a good chance you can end up with the wrong record. You should also make sure you returned exactly one record with the query.

$eid2 = "SELECT id FROM engines WHERE keyword = '$tricker_engine' LIMIT 1";
$result = mysql_query($eid2) or die(mysql_error());
if(mysql_num_rows($result) == 1) {
     $row = mysql_fetch_array($result):
     $eid = $row['id'];
} else {
     //If query returns other than exactly one record, results are either not present, or are ambiguous
}

Link to comment
Share on other sites

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.