mike12255 Posted April 18, 2011 Share Posted April 18, 2011 Im trying to figure out if a user has already downloaded something so im seeing if they have a uid (user id) in the database along with the nid (note id) that they are trying to download. I echoed out the sql and it returned this: SELECT * FROM purchases WHERE uid =1 AND nid =7 i manually ran that sql in phpmyadmin and it returned a couple rows. However my var $rows = mysql_num_rows($res2) has no value when i echo is out. Appriciate any help here is my code: <?php $sql2 = "SELECT * FROM purchases WHERE uid =".$user_id." AND nid =".$id; $res2 = mysql_query($sql2) or die (mysql_error()); $rows = mysql_num_rows($res2); ?> Quote Link to comment https://forums.phpfreaks.com/topic/234096-mysql_num_rows-not-working/ Share on other sites More sharing options...
dcro2 Posted April 18, 2011 Share Posted April 18, 2011 Are you absolutely positively sure $user_id and $id contain what you think they do? Echo $sql2 so you can see what the query actually is. Quote Link to comment https://forums.phpfreaks.com/topic/234096-mysql_num_rows-not-working/#findComment-1203187 Share on other sites More sharing options...
mike12255 Posted April 18, 2011 Author Share Posted April 18, 2011 i did that and i got the query that i qouted above Quote Link to comment https://forums.phpfreaks.com/topic/234096-mysql_num_rows-not-working/#findComment-1203195 Share on other sites More sharing options...
mikosiko Posted April 18, 2011 Share Posted April 18, 2011 can you show more code?... I don't see any echo in the posted one (not saying that you don't have it) Quote Link to comment https://forums.phpfreaks.com/topic/234096-mysql_num_rows-not-working/#findComment-1203203 Share on other sites More sharing options...
mike12255 Posted April 18, 2011 Author Share Posted April 18, 2011 I took out the echos and die statment, but here is the full code: <?php require_once("models/config.php"); if(isUserLoggedIn()) { if($id = $_GET['nid']){ $sql = "SELECT * FROM notes WHERE id = ".$id; $res = mysql_query($sql) or die (mysql_error()); while ($row = mysql_fetch_array($res)){ $noteName = $row['noteName']; $name = $row['name']; $cost = $row['cost']; $user = $loggedInUser->display_username; $sql = "SELECT * from userCake_Users WHERE Username = '$user'"; $res = mysql_query($sql) or die (mysql_error()); $user_id = getInfoFromUsername($user); $user_id = $user_id['User_ID']; $sql2 = "SELECT * FROM purchases WHERE uid =".$user_id." AND nid =".$id; $res2 = mysql_query($sql2) or die (mysql_error()); $rows = mysql_num_rows($res2); die($sql2); while ($row = mysql_fetch_array($res)){ $user_id = $row['User_ID']; if ($rows > 0){ header("Location:view.php?error=4"); } $userCredits = $row['credits']; if ($userCredits >= $cost){ $sql = "UPDATE userCake_Users SET credits = credits -".$cost." WHERE Username ='$user'"; $res = mysql_query($sql) or die (mysql_error()); $date = time(); $sql3 = "INSERT INTO `purchases` (nid,uid,`date`) VALUES('$id','$user_id','$date')"; $res3 = mysql_query($sql3) or die (msyql_error()); $location = $name; header("Location: download.php?f=".$location); }else{ header("Location:view.php?error=3"); } } } }else{ header("Location:view.php?error=2"); } }else{ header("Location:view.php?error=1"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/234096-mysql_num_rows-not-working/#findComment-1203205 Share on other sites More sharing options...
maxudaskin Posted April 18, 2011 Share Posted April 18, 2011 Try this. <?php $sql = "SELECT * FROM `purchases` WHERE `uid` = '$user_id' AND nid = '$id'"; // If you're using double quotes, you can leave the variables in there $query = mysql_query($sql) or die (mysql_error()); $row_cnt = mysql_num_rows($query); echo "The SQL query $sql returns $row_cnt rows."; Put that in. I have a feeling that it is because you didn't put your single or double quotes around the values of uid and nid. Quote Link to comment https://forums.phpfreaks.com/topic/234096-mysql_num_rows-not-working/#findComment-1203206 Share on other sites More sharing options...
mike12255 Posted April 18, 2011 Author Share Posted April 18, 2011 problem solved thanks a lot for that. Quote Link to comment https://forums.phpfreaks.com/topic/234096-mysql_num_rows-not-working/#findComment-1203208 Share on other sites More sharing options...
maxudaskin Posted April 18, 2011 Share Posted April 18, 2011 Was it the SQL string? Quote Link to comment https://forums.phpfreaks.com/topic/234096-mysql_num_rows-not-working/#findComment-1203210 Share on other sites More sharing options...
mike12255 Posted April 18, 2011 Author Share Posted April 18, 2011 not quite solved.. the echo you added says this: The SQL query SELECT * FROM `purchases` WHERE `uid` = '1' AND nid = '7' returns 13 rows. however this doesnt work it skips it: if ($row_cnt > 0){ header("Location:view.php?error=4"); } Quote Link to comment https://forums.phpfreaks.com/topic/234096-mysql_num_rows-not-working/#findComment-1203214 Share on other sites More sharing options...
maxudaskin Posted April 18, 2011 Share Posted April 18, 2011 <?php if ($row_cnt > 0){ // This is pretty much saying, "if there are more than zero rows, return an error" // I assume you want it to return an error if there is less than one row? if ($row_cnt < 1){ Quote Link to comment https://forums.phpfreaks.com/topic/234096-mysql_num_rows-not-working/#findComment-1203216 Share on other sites More sharing options...
mikosiko Posted April 18, 2011 Share Posted April 18, 2011 I see a bigger problem in your code... you are altering your resultset ($res) inside of your main loop (first while).. that could generate all kind of inconsistence... and your second while while ($row = mysql_fetch_array($res)){ $user_id = $row['User_ID']; seems to be using the wrong resultset and variables... and also looks out-off-place (if validating num_rows inside the loop???) Quote Link to comment https://forums.phpfreaks.com/topic/234096-mysql_num_rows-not-working/#findComment-1203227 Share on other sites More sharing options...
mike12255 Posted April 18, 2011 Author Share Posted April 18, 2011 ok, ive completly rewritten the file however if the user has bought the note already it still does not redirect them to view.php?error=2 here is my code: also: die($row2); // returns nothing echo ("there are a total of ".$row2. " rows"); // returns there are a total of 17 rows <?php require_once("models/config.php"); if(isUserLoggedIn()) { $id = mysql_real_escape_string($_GET['nid']); $username = $loggedInUser->display_username; $user_id = getInfoFromUsername($username); $user_credits = $user_id['credits']; $user_id = $user_id['User_ID']; $sql = "SELECT * FROM notes WHERE id = '$id'"; $res = mysql_query($sql) or die (mysql_error()); $row = mysql_fetch_array($res); $cost = $row['cost']; $filename = $row['name']; $sql2 = "SELECT * FROM purchases WHERE nid = '$id' AND uid = '$user_id'"; $res2 =mysql_query($sql2) or die (mysql_error()); $row2 = mysql_num_rows($res2); if ($row2 > 0){ //already bought the note header("Location: view.php?error=2"); } if ($row['cost'] <= $user_credits){ $new_value = $user_credits - $cost; $sql3 = "UPDATE userCake_Users SET credits = '$new_value' WHERE User_ID = '$user_id'"; $res3 = mysql_query($sql3) or die (mysql_error()); $date = time(); $sql4 = "INSERT INTO purchases (nid,uid,`date`) VALUES ('$id','$user_id','$date')"; $res4 = mysql_query($sql4) or die (mysql_error()); header("Location: download.php?f=".$filename); }else{ //not enough credits header("Location: view.php?error=3"); } }else{ //not logged in header("Location: view.php?error=1"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/234096-mysql_num_rows-not-working/#findComment-1203249 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.