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 Link to comment https://forums.phpfreaks.com/topic/94249-mysql_query-question-due-to-search-not-working/ 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! Link to comment https://forums.phpfreaks.com/topic/94249-mysql_query-question-due-to-search-not-working/#findComment-482733 Share on other sites More sharing options...
revraz Posted March 4, 2008 Share Posted March 4, 2008 It's mysql_num_rows Link to comment https://forums.phpfreaks.com/topic/94249-mysql_query-question-due-to-search-not-working/#findComment-482752 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. Link to comment https://forums.phpfreaks.com/topic/94249-mysql_query-question-due-to-search-not-working/#findComment-482779 Share on other sites More sharing options...
BlueSkyIS Posted March 4, 2008 Share Posted March 4, 2008 deprecated. Link to comment https://forums.phpfreaks.com/topic/94249-mysql_query-question-due-to-search-not-working/#findComment-482812 Share on other sites More sharing options...
RichG Posted March 7, 2008 Author Share Posted March 7, 2008 Thanks guys working ok now Link to comment https://forums.phpfreaks.com/topic/94249-mysql_query-question-due-to-search-not-working/#findComment-485990 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.