RichG Posted March 4, 2008 Share Posted March 4, 2008 Hi Just tried to search the site and the search engine seems to be broken at the moment. I am querying a database and displaying return result based on a id number enterd on a prior page. I have retreived the data and displayed it but I now wnat to validate the request by checking whether the data exists. Meaning of the search os for a number not in the database it will return "no such model in database" I have tried a few things but just get a blank page. This is the code that works ok i am just unsure of where my new code will go to perform the task. // This is the databse query that returns the model request $query = "SELECT modelid, fname, lname FROM models WHERE modelid = $modelid"; $result = mysql_query($query); while ($row = mysql_fetch_object($result)){ echo "<h2>Here is your model:</h2><br>\n Model ID: $row->modelid<br>\n First Name: $row->fname<br>\n Second Name: $row->lname<br>\n"; } Cheers RichG Quote Link to comment Share on other sites More sharing options...
Agricola Posted March 4, 2008 Share Posted March 4, 2008 You use mysql_numrows count how many returned records, if 0 then skip else process <?php $query = "SELECT modelid, fname, lname FROM models WHERE modelid = $modelid"; $result = mysql_query($query); $records_exist= mysql_numrows($result); //CHECK RECORDS HAVE BEEN RETURNED if ($records_exist > 0 ){ //use if else statement to carry out required actions while ($row = mysql_fetch_object($result)){ echo "<h2>Here is your model:</h2> \n Model ID: $row->modelid \n First Name: $row->fname \n Second Name: $row->lname \n"; } } else { // if statement //do whatever task if no record exists here echo "no such model in database"; // your required error message! } ?> EDIT : some reason this posted when i hit enter key, only now is this complete! Quote Link to comment Share on other sites More sharing options...
revraz Posted March 4, 2008 Share Posted March 4, 2008 It's mysql_num_rows Quote Link to comment Share on other sites More sharing options...
Agricola Posted March 4, 2008 Share Posted March 4, 2008 It's mysql_num_rows Yes should be using mysql_num_rows, mysql_numrows will still work, but is depreciated now and should only be used for downward compatability. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 4, 2008 Share Posted March 4, 2008 deprecated. Quote Link to comment Share on other sites More sharing options...
RichG Posted March 7, 2008 Author Share Posted March 7, 2008 Thanks guys working ok now 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.