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");
		}

 

Link to comment
Share on other sites

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.";
			}
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.