Jump to content

[SOLVED] need help displaying error/message when no match found in mysql db


jeger003

Recommended Posts

hello everyone,

i am trying to create a list of items but im not really sure how i can make it display a message if there are no items to display

 

here is wat i have so far....i just need something that will display when there are no items found in the db

<?php


include "db.php";

$get_subject = $_GET['orderedby'];


$query = mysql_query("SELECT * FROM listings WHERE search_text LIKE '%$get_subject%' AND live = 1") or die(mysql_error());

while ($fetch = mysql_fetch_array($query))
{
	echo $fetch['title']."<br>";

}



?>

Use mysql_num_rows to determine if more than 0 rows have been found.

 


$query = mysql_query("SELECT * FROM listings WHERE search_text LIKE '%$get_subject%' AND live = 1") or die(mysql_error());

$num = mysql_num_rows($query);	
if($num > 0) {
while ($fetch = mysql_fetch_array($query))
{
  echo $fetch['title']."<br>";
}
else
echo 'No matches found';

Use mysql_num_rows to determine if more than 0 rows have been found.

 


$query = mysql_query("SELECT * FROM listings WHERE search_text LIKE '%$get_subject%' AND live = 1") or die(mysql_error());

$num = mysql_num_rows($query);	
if($num > 0) {
while ($fetch = mysql_fetch_array($query))
{
  echo $fetch['title']."<br>";
}
else
echo 'No matches found';

 

 

awesome work man!!!!

THANKS!!

hey guys i have one more question

 

as you can see i am gettin a value from the url called orderedby......i want to be able to use different statements for each value..........values are orderedby=1, orderedby=2.......orderedby=6

 

i was thinking maybe using elseif???

 

this is what i put together........is this a proper way to code??? reason why i want to do it like this is so that the user wont be able to just enter any value.....the values for $_GET would be locked.

 

<?php


include "db.php";

$get_subject = $_GET['orderedby'];

if($get_subject == '1')
{

$value_to_find = 'car';

$query = mysql_query("SELECT * FROM lisitings WHERE search_text LIKE '%$value_to_find%' AND live = 1") or die(mysql_error());


$num = mysql_num_rows($query);   
if($num > 0) {
 while ($fetch = mysql_fetch_array($query))
 {
  echo $fetch['title']."<br>";
 }
 }
else
{
echo 'There are no listing for that subject!';
}
}
elseif ($get_subject == '2')
{
$value_to_find = 'house';
$query = mysql_query("SELECT * FROM lisitings WHERE search_text LIKE '%$value_to_find%' AND live = 1") or die(mysql_error());


$num = mysql_num_rows($query);   
if($num > 0) {
 while ($fetch = mysql_fetch_array($query))
 {
  echo $fetch['title']."<br>";
 }
 }
else
{
echo 'There are no listing for that subject!';
}
elseif ($get_subject == '3')
{
$value_to_find = 'cloths';
$query = mysql_query("SELECT * FROM lisitings WHERE search_text LIKE '%$value_to_find%' AND live = 1") or die(mysql_error());


$num = mysql_num_rows($query);   
if($num > 0) {
 while ($fetch = mysql_fetch_array($query))
 {
  echo $fetch['title']."<br>";
 }
 }
else
{
echo 'There are no listing for that subject!';
}	
}

// then i would just continue to copy and paste it below until i get to $get_subject == 6 and continue to use elseif

?>


 

You should avoid writing duplicate code. 

 

Use a switch statement to catch the value of orderby and set the value of value_to_find.

 

You also need to make certain you are santizing the url var.  Using is_nueric() then make sure it's a valid orderby value before using it. 

 

Here is a good start

<?php
if(!is_numeric($_GET['orderedby']))
die('orderedby was not a number');

include "db.php";
$get_subject = $_GET['orderedby'];

switch($get_subject)
{
	case 1;
		$value_to_find = 'car';
		break;
	case 2;
		$value_to_find = 'house';
		break;
	case 3;
		$value_to_find = 'cloths';
		break;
}

$query = mysql_query("SELECT * FROM lisitings WHERE search_text LIKE '%$value_to_find%' AND live = 1") or die(mysql_error());
$num = mysql_num_rows($query);   

if($num > 0) 
{
 while ($fetch = mysql_fetch_array($query))
	{
		echo $fetch['title']."<br>";
	}
}

else
{
	echo 'There are no listing for that subject!';
}

?>

wow this is a very nice way of doing it

 

could the switch be used if i wanted the url to have letters instead of numbers? like say it was orderedby=car instead of orderedby=1.....see i want to prevent the user from putting any words after the orderedby=  cause it would run the query and actually find fields that match.

 

also in "if(!is_numeric($_GET['orderedby']))" aren't you saying if orderedby is not numeric?

 

thank you for the help

 

wow this is a very nice way of doing it

 

could the switch be used if i wanted the url to have letters instead of numbers? like say it was orderedby=car instead of orderedby=1.....see i want to prevent the user from putting any words after the orderedby=  cause it would run the query and actually find fields that match.

 

also in "if(!is_numeric($_GET['orderedby']))" aren't you saying if orderedby is not numeric?

 

thank you for the help

 

 

Yes switch case can be used with strings or integers. If the value is a string make sure to quote the case values.  example case 'car'; 

 

You can change it up how ever you want just make sure you validate all data before allowing it to be used within a query. 

 

and yes "if(!is_numeric($_GET['orderedby']))"does say is this not a number and the next line says die. In your question you used numbers therefore this code would make sure only a number is being used and if not end the script because someone changed the url var to something other than a number. If you wanted to use strings instead of integers this section of code would need to be changed to fit.

 

 

wow this is a very nice way of doing it

 

could the switch be used if i wanted the url to have letters instead of numbers? like say it was orderedby=car instead of orderedby=1.....see i want to prevent the user from putting any words after the orderedby=  cause it would run the query and actually find fields that match.

 

also in "if(!is_numeric($_GET['orderedby']))" aren't you saying if orderedby is not numeric?

 

thank you for the help

 

 

Yes switch case can be used with strings or integers. If the value is a string make sure to quote the case values.  example case 'car'; 

 

You can change it up how ever you want just make sure you validate all data before allowing it to be used within a query. 

 

and yes "if(!is_numeric($_GET['orderedby']))"does say is this not a number and the next line says die. In your question you used numbers therefore this code would make sure only a number is being used and if not end the script because someone changed the url var to something other than a number. If you wanted to use strings instead of integers this section of code would need to be changed to fit.

 

ah that makes sense.......i was confused at first.......but checking if its a number or not is VERY helpful technique.......thanks soo much for your help!

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.