Jump to content

mysql_num_rows not working.


mike12255

Recommended Posts

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);
				?>

 

Link to comment
Share on other sites

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


?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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???)

 

 

Link to comment
Share on other sites

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

	}


?>

 

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.