stualk Posted July 19, 2007 Share Posted July 19, 2007 I'm trying to return a "Not found" page if a record isn't found in my db. The first bit of my code works fine where if the record is found it goes to the correct page and displays the results, it's the second bit that doesn't. I used to use the following code, which worked fine until php5 came about! What can I do to make it work now in php5? <?php $connection = mysql_connect("localhost","user","pass") or die ("No Server"); $db = mysql_select_db("cards_db", $connection) or die("No DataBase"); $sql = "select * from cards where card_number='$card_number'"; $result = mysql_query($sql) or die("No Query"); $num = mysql_numrows($result); for ($i=1; $i<=1; $i++) { if ($row = mysql_fetch_array($result)) { $card_number = $row["card_number"]; if ($num == 1) { header("Location: index_returned.php?card_number=$card_number"); } else { header("Location: index_wrong.php"); } } } ?> Quote Link to comment Share on other sites More sharing options...
matto Posted July 19, 2007 Share Posted July 19, 2007 Your mysql_numrows($result); should be: mysql_num_rows($result); Quote Link to comment Share on other sites More sharing options...
matto Posted July 19, 2007 Share Posted July 19, 2007 Also, where is $card_number coming from: $sql = "select * from cards where card_number='$card_number'"; this should probably be $_GET['card_number'] or $_POST['card_number'] Note to sure why you have a for loop in your code..... Quote Link to comment Share on other sites More sharing options...
stualk Posted July 19, 2007 Author Share Posted July 19, 2007 card_number comes from a submission form on the previous page. There is one field labelled 'card_number' and when submitted it carries the value of that field across to select from the db. This works perfectly if the record exists and it displays the results. If it finds nothing it's not doing the last bit of my code. I have adjusted as you suggested but it still doesn't work. My code currently looks like this, anything else it could be?: <?php $card_number = $_GET['card_number']; $connection = mysql_connect("localhost","user","pass") or die ("No Server"); $db = mysql_select_db("cards_db", $connection) or die("No DataBase"); $sql = "select * from cards where card_number='$card_number'"; $result = mysql_query($sql) or die("No Query"); $num = mysql_num_rows($result); for ($i=1; $i<=1; $i++) { if ($row = mysql_fetch_array($result)) { $card_number = $row["card_number"]; if ($num == 1) { header("Location: index_returned.php?card_number=$card_number"); } else { header("Location: index_wrong.php"); } } } ?> Quote Link to comment Share on other sites More sharing options...
matto Posted July 19, 2007 Share Posted July 19, 2007 This may do the trick - note it's not tested.... $connection = mysql_connect("localhost","user","pass") or die ("No Server"); mysql_select_db("cards_db", $connection) or die("No DataBase"); $sql = "select card_number from cards where card_number= '" . $_GET['card_number'] . "' limit 1"; $result = mysql_query($sql) or die("No Query"); if(mysql_num_rows($result) > 0) { $row = mysql_fetch_object($result); header("Location: index_returned.php?card_number=$row->card_number"); } else { header("Location: index_wrong.php"); } Quote Link to comment Share on other sites More sharing options...
stualk Posted July 19, 2007 Author Share Posted July 19, 2007 Almost, that code is taking me to the 'not found' page whether the record is found in the db or not. Does it need a slight alteration of some sort? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 19, 2007 Share Posted July 19, 2007 that code looks good to me. it should work... $connection = mysql_connect("localhost","user","pass") or die ("No Server"); mysql_select_db("cards_db", $connection) or die("No DataBase"); $sql = mysql_query("SELECT * FROM `cards` WHERE `card_number`={$_GET['card_number']}"); echo mysql_num_rows($qry); //this is to see how much rows are returned. exit(); //remove this when it is doing it properly. if(mysql_num_rows($qry) == 0) { header("Location: index_wrong.php"); }else{ $row = mysql_fetch_array($sql); header("Location: index_returned.php?card_number={$row['card_number']}"); } Quote Link to comment Share on other sites More sharing options...
stualk Posted July 19, 2007 Author Share Posted July 19, 2007 Still not quite doing it. That returns an error. Do I need to add $_POST['card_number'] to the page where the users enters the card number perhaps? Quote Link to comment Share on other sites More sharing options...
chadrt Posted July 19, 2007 Share Posted July 19, 2007 Just a thought here. Seems that you should be using everything in a single page here show your error in the same page you are sending the data to would simplify things a bunch. Also if your server is ever down then using statements like exit; or die will cause the rest of the script not to colme to a halt. If you use an error trap you just ignore the code that is meant for the query and then even redirect them to another error page or use that page to show the error. Sample for your question above: (Untested!!) <?php $card_number = $_GET['card_number']; $connection = mysql_connect("localhost","user","pass") or die ("No Server"); $db = mysql_select_db("cards_db", $connection) or die("No DataBase"); $sql = "select * from cards where card_number='$card_number'"; $result = mysql_query($sql) or die("No Query"); $num = mysql_numrows($result); for ($i=1; $i<=1; $i++) { if ($row = mysql_fetch_array($result)) { $card_number = $row["card_number"]; if ($num == 0) { # redirection if $num = no result header( 'Location: http://www.yoursite.com/new_page.html' ) ; } else { # otherwise redirect here header("Location: index_returned.php?card_number=$card_number"); } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.