dominod Posted August 2, 2010 Share Posted August 2, 2010 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 https://forums.phpfreaks.com/topic/209575-getting-the-id-from-a-mysql-table/ Share on other sites More sharing options...
bh Posted August 2, 2010 Share Posted August 2, 2010 Use while statement. while ($row = mysql_fetch_assoc($eid2)) { echo $row['id']; } Link to comment https://forums.phpfreaks.com/topic/209575-getting-the-id-from-a-mysql-table/#findComment-1094071 Share on other sites More sharing options...
Yesideez Posted August 2, 2010 Share Posted August 2, 2010 When you're using wildcards (%) with strings you need to use LIKE instead of equals (=) $eid2 = mysql_query("SELECT id FROM engines WHERE keyword LIKE '%".$tricker_engine."%' LIMIT 1") or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/209575-getting-the-id-from-a-mysql-table/#findComment-1094072 Share on other sites More sharing options...
dominod Posted August 2, 2010 Author Share Posted August 2, 2010 Fixed it with $row = mysql_fetch_array($eid2); I also removed the wildcards .. Have no idea what they are good for anyways Thanks alot guys Link to comment https://forums.phpfreaks.com/topic/209575-getting-the-id-from-a-mysql-table/#findComment-1094186 Share on other sites More sharing options...
Pikachu2000 Posted August 2, 2010 Share Posted August 2, 2010 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 https://forums.phpfreaks.com/topic/209575-getting-the-id-from-a-mysql-table/#findComment-1094189 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.