Forum Moderators: coopster

Message Too Old, No Replies

mysql_num_rows problem

         

hughes

12:16 pm on Aug 8, 2005 (gmt 0)

10+ Year Member



Hi, I keep getting an error result saying:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in *** on line 9

Warning: Cannot modify header information - headers already sent by (output started at \***:9) in **** on line 12

I'm pretty new to PHP so I'm thinking its a problem with syntax. Any help is appreciated. Code is below.

<?PHP

$ni = trim($_POST['ni']);

require 'db_connect.php';

$sql = "SELECT * FROM $db_table WHERE ni = '$ni'";
$query = "mysql_query($sql) or die (mysql_error())";
if (mysql_num_rows($query) < 1) {

require 'db_close.php';
header("location: DESIRED LOCATION");
}
else{
require 'db_close.php';
header("Location: EXIT ");
}

?>

mcibor

1:27 pm on Aug 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This should work:
<?php

$ni = trim($_POST['ni']);

require 'db_connect.php';

$sql = "SELECT * FROM $db_table WHERE ni = '$ni'";
$query = mysql_query($sql) or die (mysql_error());//No quotes here!
if (mysql_num_rows($query) < 1) {

require 'db_close.php';
header("location: http://www.example.com/go_on.html");//here should be url, no text
}
else{
require 'db_close.php';
header("Location: http://www.example.com/exit.html");
}
?>


Make sure, that before <? there is nothing, not even space!

Best regards
Michal Cibor

dreamcatcher

1:39 pm on Aug 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Both of the errors are related. You cannot send data to the browser before you call the header function. Because a message is being sent to due to the num rows error, this is why you are getting the headers not sent error.

Apply the fix shown by mcibor and it should be fine.

dc

hughes

2:09 pm on Aug 8, 2005 (gmt 0)

10+ Year Member



That worked a treat. Thanks a lot.