Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-16-2010, 07:24 PM   PM User | #1
kimboslice
Regular Coder

 
Join Date: Dec 2009
Posts: 102
Thanks: 18
Thanked 0 Times in 0 Posts
kimboslice is an unknown quantity at this point
shoulnt this work?

this script shows results from a search , im trying to add a if else statement to let the users know if no matches was found
shouldnt the code work? when a search matches it shows the fields from the table but when nothing match it doesnt show the else statement

thanks Kimbo




<body>
<?php

$con = mysql_connect("localhost","xxxx","xxxxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}



mysql_select_db("xxxxxx", $con);




$search =check($_POST['search'], "you havent enterd anything , <a href=personal.php>go back</a> ");




function check($data, $error='')

{

$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($error && strlen($data) == 0) {
die($error);
}
return $data;
}




$search=$_POST["search"];


$result = mysql_query("SELECT * FROM personal WHERE name LIKE '%$search%'");


while($r=mysql_fetch_array($result))
{


$name=$r["name"];
$adress=$r["adress"];
$areacode=$r["areacode"];
$phonenumber=$r["phonenumber"];
$email=$r["email"];
$id=$r["id"];

if($result){ //works

echo "$name <br> $adress <br> $area code <br> $phonenumber<br />
$email <br>";
}

else {
echo "no records was found"; //dont work
}




}

?>
kimboslice is offline   Reply With Quote
Old 05-16-2010, 07:29 PM   PM User | #2
met
Regular Coder

 
Join Date: Oct 2009
Location: United Kingdom
Posts: 728
Thanks: 4
Thanked 119 Times in 119 Posts
met has a little shameless behaviour in the past
the reason it doesn't work is that providing a valid SQL query is provided, mysql_query will return true, regardless of results.

even if no results are found, it's still "true", so the else condition never occurs.

[snip, misread your code]

PHP Code:
$result mysql_query("SELECT * FROM personal WHERE name LIKE '%$search%'");
$num_results=mysql_num_rows($result);

/* get results.. */

if($num_results>0) { // there are results..

    
while($r=mysql_fetch_array($result))
    {
            
$name=$r["name"];
            
$adress=$r["adress"];
            
$areacode=$r["areacode"];
            
$phonenumber=$r["phonenumber"];
            
$email=$r["email"];
            
$id=$r["id"];
            echo 
"$name <br> $adress <br> $area code <br> $phonenumber<br /> $email <br>";
    } 
// while

} else {

    echo 
"no records was found"//dont work



Last edited by met; 05-16-2010 at 07:34 PM..
met is offline   Reply With Quote
Users who have thanked met for this post:
kimboslice (05-16-2010)
Old 05-16-2010, 07:51 PM   PM User | #3
kimboslice
Regular Coder

 
Join Date: Dec 2009
Posts: 102
Thanks: 18
Thanked 0 Times in 0 Posts
kimboslice is an unknown quantity at this point
thanks alot
kimboslice is offline   Reply With Quote
Old 05-16-2010, 11:20 PM   PM User | #4
kimboslice
Regular Coder

 
Join Date: Dec 2009
Posts: 102
Thanks: 18
Thanked 0 Times in 0 Posts
kimboslice is an unknown quantity at this point
it returns else statement now both when the record doesnt match and when it does . any suggestions?
kimboslice is offline   Reply With Quote
Old 05-16-2010, 11:30 PM   PM User | #5
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
Post your latest code.
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ is offline   Reply With Quote
Old 05-16-2010, 11:53 PM   PM User | #6
kimboslice
Regular Coder

 
Join Date: Dec 2009
Posts: 102
Thanks: 18
Thanked 0 Times in 0 Posts
kimboslice is an unknown quantity at this point
thanks, current code:




<div >
<?php

include ('db_connect.php');

$search = check($_POST['search'], "you didnt enter anything, <a href=personal.php>go back</a> ");




function check($data, $error='') {

$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($error && strlen($data) == 0) / {

die($error);
}
return $data;
}




$search=$_POST["search"];


$result = mysql_query("SELECT * FROM personal WHERE name LIKE '%$search%'");

if($num_results>0)

while($r=mysql_fetch_array($result))
{


$namn=$r["name"];
$adress=$r["adress"];
$postnummer=$r["areacode"];
$telefonnummer=$r["phonenumber"];
$email=$r["email"];
$id=$r["id"];



echo "$name<br> $adress <br> $areacode<br> $phonenumber<br />
$email <br>";

}
else {

echo "no records was found";

}



?>
kimboslice is offline   Reply With Quote
Old 05-17-2010, 12:11 AM   PM User | #7
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
Looks like you missed this line
PHP Code:
$num_results=mysql_num_rows($result); 
It goes just after $result.
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ is offline   Reply With Quote
Users who have thanked _Aerospace_Eng_ for this post:
kimboslice (05-17-2010)
Old 05-17-2010, 12:41 AM   PM User | #8
kimboslice
Regular Coder

 
Join Date: Dec 2009
Posts: 102
Thanks: 18
Thanked 0 Times in 0 Posts
kimboslice is an unknown quantity at this point
thanks to many hours in front of the computer
kimboslice is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:04 PM.


Advertisement
Log in to turn off these ads.