pFirst time using a DB(MYSQL) and php to produce a little search.
I have a list of zip codes for sales reps. The intial search worked great, but I want to have an if/else so if someone enters a zip code that is not in my database they are taken to a different page or recieve a message.
Not haveing much luck.
Here is the code I have:
<?php
mysql_connect ("localhost", "peterson_galiant","p3t3rs0n") or die (mysql_error());
mysql_select_db ("peterson_zipcode");
$term = $_POST['term'];
$sql = mysql_query("select * from reps_zip where zip like '%$term%'");
if ($row = mysql_fetch_array($sql)){
echo '<img src="'.$row['picture'].'" width=274>';
echo '<br/>'.$row['name'];
echo '<br/> <a href="'.$row['page'].'">Contact Info</a>';
echo '<br/><br/>';
} else {
// the query executed, check if there are any rows in the result set
if(mysql_num_rows($row) > 0){ echo '<br/>TEST<br/>';
// code to execute when there are any row(s) in the result set
}
}
?>
num_rows should be executed on the RESULT. you’re executing it on the ROW. (Hint: you’ll have more luck replacing $row with $sql in your second if).
You’re using wildcard searching (LIKE) and then only pulling 1 result - are you intending on allowing partial matches, and if so, why are you only pulling one result?
Try flipping the first IF around.
IF num_rows == 0 {
do your non-row stuff
} ELSE {
$row = fetch row
do your row stuff
}
ok, flopped the two.
First try at PHP and MYSQL, so some of what you mentioned about result and like lost me.
I am not trying to pull partial results and I only want one results (though I cleaned up all duplicated from DB). So one zip code goes to a specific sales rep.
Switching and changing to sql makes the word test show up, but for all searches even those that have data associated with them.
<?php
mysql_connect ("localhost", "peterson_galiant","p3t3rs0n") or die (mysql_error());
mysql_select_db ("peterson_zipcode");
$term = $_POST['term'];
$sql = mysql_query("select * from reps_zip where zip like '%$term%'");
if(mysql_num_rows($sql) > 0){ echo '<br/>TEST<br/>';
} else {if ($row = mysql_fetch_array($sql)){
echo '<img src="'.$row['picture'].'" width=274>';
echo '<br/>'.$row['name'];
echo '<br/> <a href="'.$row['page'].'">Contact Info</a>';
echo '<br/><br/>';
}
}
?>
I have the search code working great if you search for a zip that is in the database it works fine.
What I am trying to do is if the zip code searched for is not in the database I want to post a message or page that the search returned no results.
Here is the code that works which doesn’t include and option for no results:
<?php
mysql_connect ("localhost", "peterson_galiant","p3t3rs0n") or die (mysql_error());
mysql_select_db ("peterson_zipcode");
$term = $_POST['term'];
$sql = mysql_query("select * from reps_zip where zip like '%$term%'");
while ($row = mysql_fetch_array($sql)){
echo '<img src="'.$row['picture'].'" width=274>';
echo '<br/>'.$row['name'];
echo '<br/> <a href="'.$row['page'].'">Contact Info</a>';
echo '<br/><br/>';
}
?>
EDIT:: * on further reading, the way you’ve done it, it would work either way…
Also, your first IF checks for the number of rows being more than 0. If you want that piece of code to be executed when there are no results, you’ll need:
The second if is unnecessary - if there is a row (mysql_num_rows != 0), then $row = mysql_fetch_array($sql) will always be true.
if(mysql_num_rows($sql) == 0) {
//No Rows were Pulled. Either a missing zip code, or a bad query.
} else {
$row = mysql_fetch_array($sql); //Will always work because there is at least 1 row.
// Result found.
}