Jump to content

mysql_query question due to search not working


RichG

Recommended Posts

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

 

 

 

 

 

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.