mike12255 Posted April 21, 2011 Share Posted April 21, 2011 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"); } Quote Link to comment https://forums.phpfreaks.com/topic/234310-mysql_num_rows-not-working-with-die-statment-or-if-statment/ Share on other sites More sharing options...
dan_t Posted April 21, 2011 Share Posted April 21, 2011 It seems like you would still have to use some kind of loop for the mysql_num_rows function to work. Quote Link to comment https://forums.phpfreaks.com/topic/234310-mysql_num_rows-not-working-with-die-statment-or-if-statment/#findComment-1204321 Share on other sites More sharing options...
Pikachu2000 Posted April 21, 2011 Share Posted April 21, 2011 What happens if you get rid of the die()s and add echo "Header"; on the line before the header() redirect? Does it output 'Header'? Quote Link to comment https://forums.phpfreaks.com/topic/234310-mysql_num_rows-not-working-with-die-statment-or-if-statment/#findComment-1204323 Share on other sites More sharing options...
mike12255 Posted April 21, 2011 Author Share Posted April 21, 2011 echoing header did get it inside the if statment that it was suposed to but then i can send headers because output. Im really confused about this issue, never had it before. Quote Link to comment https://forums.phpfreaks.com/topic/234310-mysql_num_rows-not-working-with-die-statment-or-if-statment/#findComment-1204326 Share on other sites More sharing options...
Skewled Posted April 21, 2011 Share Posted April 21, 2011 You can't have any output before the header change, so you can't echo anything at all. You could do an ELSE and then perform your echo statements. Quote Link to comment https://forums.phpfreaks.com/topic/234310-mysql_num_rows-not-working-with-die-statment-or-if-statment/#findComment-1204327 Share on other sites More sharing options...
mike12255 Posted April 21, 2011 Author Share Posted April 21, 2011 i know about that, i think pikachu just wanted me to do that to test somthing. but this page also does not echo anything it simply validates and redirects. Quote Link to comment https://forums.phpfreaks.com/topic/234310-mysql_num_rows-not-working-with-die-statment-or-if-statment/#findComment-1204328 Share on other sites More sharing options...
Pikachu2000 Posted April 21, 2011 Share Posted April 21, 2011 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."; } } Quote Link to comment https://forums.phpfreaks.com/topic/234310-mysql_num_rows-not-working-with-die-statment-or-if-statment/#findComment-1204330 Share on other sites More sharing options...
mike12255 Posted April 21, 2011 Author Share Posted April 21, 2011 never mind just checked somthing it gets inside the if statment ($row2 > 0) but does not get inside the (!headers_sent) one Quote Link to comment https://forums.phpfreaks.com/topic/234310-mysql_num_rows-not-working-with-die-statment-or-if-statment/#findComment-1204331 Share on other sites More sharing options...
Pikachu2000 Posted April 21, 2011 Share Posted April 21, 2011 Not sure what you mean by that . . . Quote Link to comment https://forums.phpfreaks.com/topic/234310-mysql_num_rows-not-working-with-die-statment-or-if-statment/#findComment-1204332 Share on other sites More sharing options...
mike12255 Posted April 21, 2011 Author Share Posted April 21, 2011 i modified the post above this one to explain Quote Link to comment https://forums.phpfreaks.com/topic/234310-mysql_num_rows-not-working-with-die-statment-or-if-statment/#findComment-1204334 Share on other sites More sharing options...
PFMaBiSmAd Posted April 21, 2011 Share Posted April 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234310-mysql_num_rows-not-working-with-die-statment-or-if-statment/#findComment-1204335 Share on other sites More sharing options...
mike12255 Posted April 21, 2011 Author Share Posted April 21, 2011 You got it, I needed an exit; after sending my headers. I never knew that could happened I assumed that sending headers just basically stopped the script itself, thanks to all who helped me. Quote Link to comment https://forums.phpfreaks.com/topic/234310-mysql_num_rows-not-working-with-die-statment-or-if-statment/#findComment-1204336 Share on other sites More sharing options...
PFMaBiSmAd Posted April 21, 2011 Share Posted April 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234310-mysql_num_rows-not-working-with-die-statment-or-if-statment/#findComment-1204338 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.