Jump to content

Mysql_Num_rows not working with die statment or if statment


mike12255

Recommended Posts

So im trying to figure out if a user is already in the table so im checking for the number of rows where the user id shows up on a record and if its greater then one redirect them. My problem is the if statment does not work and I know there is more then one row. Also die ($row2); wont output data but it will in a sentence (see code below for example)

		$sql2 = "SELECT * FROM purchases WHERE nid = '$id' AND uid = '$user_id'";
		$res2 =mysql_query($sql2) or die (mysql_error());
		$row2 = mysql_num_rows($res2);

		die($row2);//no output
		die("There are a total of ".$row2." rows");//outputs There are a total of 20 rows

		if ($row2 > 0){//if statment doesnt work.
			//already bought the note
			header("Location: view.php?error=2");
		}

 

Yup. That verified that the if() conditional is TRUE. Is view.php in the same directory as the script that the code above is from? What do you get if you change the code to this:

if ($row2 > 0){//if statment doesnt work.
			//already bought the note
			if(!headers_sent() ) {
			header("Location: view.php?error=2");
			} else {
			echo "Headers already sent.";
			}
}

I'm going to venture two guesses -

 

1) The view.php page is being redirected to, but it is redirecting back to the code being posted in this thread, but because the $_POST data is no longer present, the code being posted in this thread appears to fail.

 

2) You have some code, after the code you have been posting, that is doing something while the browser is performing the redirect that makes it look like the code being posted in this thread is failing. You (almost always) need an exit; statement after a header() redirect.

 

If you post all the code on the page you are trying to troubleshoot and the code in the view.php page, someone can probably help you find out what is happening.

This is the description from the php.net documentation for the header() statement -

 

  Quote
Description

 

header() is used to send a raw HTTP header

 

^^^ That's all it does.

 

In programming, you cannot assume what anything does. If you don't know exactly what something is or does, you must read the documentation.

 

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.