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

 

 

 

 

 

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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