jbrill Posted November 26, 2007 Share Posted November 26, 2007 hey guys, i need to know how to check to see if an id exists in a table. if the id does not exist then display "some text" else continue with script. my table is called "orders" and i just want to check if the row "id" exists thanks in advance! Quote Link to comment Share on other sites More sharing options...
phpSensei Posted November 26, 2007 Share Posted November 26, 2007 $id can be $_GET['id']; I am just using number 1 as an example. <?php $id = 1; // This can be $_GET['id'] FROM the URL Values $search = mysql_query("SELECT * FROM orders WHERE id = '$id'"); // Here we select the rows with the id of 1 while($row = mysql_fetch_array($search)){ // We loop through the data if($id == $row['id']){ // Check if the id exists in the table echo " IT DOES!!!"; // yes it does } else { echo " The id $id doesnt exist"; //else no } } ?> Quote Link to comment Share on other sites More sharing options...
jbrill Posted November 26, 2007 Author Share Posted November 26, 2007 thank you! Quote Link to comment Share on other sites More sharing options...
toplay Posted November 26, 2007 Share Posted November 26, 2007 Based on phpSensei code, here's a shorter way. It sets a boolean variable ($blnFound) to true if the ID exists. <?php $id = 1; // Set $id $objResult = mysql_query("SELECT id FROM `orders` WHERE id = '$id'"); $blnFound = (boolean) ($objResult) ? mysql_num_rows($objResult) : FALSE; ?> Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 26, 2007 Share Posted November 26, 2007 Based on phpSensei code, here's a shorter way. It sets a boolean variable ($blnFound) to true if the ID exists. <?php $id = 1; // Set $id $objResult = mysql_query("SELECT id FROM `orders` WHERE id = '$id'"); $blnFound = (boolean) ($objResult) ? mysql_num_rows($objResult) : FALSE; ?> does it really important to set that var as bool when your var is returning bool i mean mysql_query returns t/f Quote Link to comment Share on other sites More sharing options...
toplay Posted November 26, 2007 Share Posted November 26, 2007 does it really important to set that var as bool when your var is returning bool i mean mysql_query returns t/f The mysql_query() function does not return false when you do a successful "SELECT" query. See manual for details: http://us2.php.net/manual/en/function.mysql-query.php FYI: Executing a successful select query does not necessarily mean there are rows (data) returned. The search criteria (in the "where" clause) might not match any data. You have to follow the select query with a fetch to see if there really is data, or in this case, we don't need the data per say but just need to know if it's there (a match was made); so I used the mysql_num_rows() to tell if there is data (without having to actually fetch the data). hth. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.