Jump to content


Photo

mysql_numrows


  • Please log in to reply
8 replies to this topic

#1 mrmoz

mrmoz
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 31 August 2006 - 03:58 PM

Hi I desperatly need this script to work but it always dies on the mysql_numrows line I have tried mysql_num_rows and if the die is php_error() I just get a blank screen.

Thanks guys

 
$sql = "SELECT cart_id, stock_id, qty, price  FROM t01_cart WHERE cart_id = '".GetCartId()."' ";
		
		$result = mysql_query($sql, $dbLink)or die ('sql error');
		
		echo $result;
		
		//checks if basket is empty
		$num = mysql_numrows($result) or die ( mysql_error() );



#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 31 August 2006 - 04:01 PM

cheek the query ok not the result?

good luck

<?php

$sql = "SELECT cart_id, stock_id, qty, price  FROM t01_cart WHERE cart_id = '".GetCartId()."' ";

echo $sql;

$result = mysql_query($sql, $dbLink)or die ('sql error');
		
if(mysql_num_rows($result)){

echo "ok";

}else{

echo "wrong";

}
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 mrmoz

mrmoz
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 31 August 2006 - 04:20 PM

Thanks but I've still got the same issues what I need is the value $num to say the number of rows returned by the query here is the code again:

<? php
$sql = "SELECT cart_id, stock_id, qty, price  FROM t01_cart WHERE cart_id = '".GetCartId()."' ";

$result = mysql_query($sql, $dbLink)or die ('sql error');

$num = mysql_num_rows($result) or die ('sql numrows error');

		
		if ($num = '0'){
		echo 'Your basket is empty';
		echo $num;} ?>


#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 31 August 2006 - 04:24 PM

Use two = and not 1. Otherwise you'll be assign $num to the string '0'. WIth two equal signs you are comparing whats on the left to the right.
<? php
$sql = "SELECT cart_id, stock_id, qty, price  FROM t01_cart WHERE cart_id = '".GetCartId()."' ";

$result = mysql_query($sql, $dbLink) or die ('sql error<br />' . mysql_error());

if (mysql_num_rows($result) == 0)
{
    echo 'Your basket is empty';
    echo $num;
} ?>


#5 mrmoz

mrmoz
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 31 August 2006 - 04:43 PM

Thanks but the point of the script is for $num to be the number of rows in the table i.e. the last line I get the or die statement as a reuslt from the last line.  Thanks

$sql = "SELECT cart_id, stock_id, qty, price  FROM t01_cart WHERE cart_id = '".GetCartId()."' ";

$result = mysql_query($sql, $dbLink) or die ('sql error<br />' . mysql_error());

$num = mysql_numrows ($result) or die ('sql error<br />' . mysql_error());

#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 31 August 2006 - 04:45 PM

Swap:
if (mysql_num_rows($result) == 0)

With this:
$num = mysql_num_rows($result);
if($num == 0)


#7 mrmoz

mrmoz
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 31 August 2006 - 04:52 PM

Thanks but it didn't work I still get the same problem it says the basket is empty but it isn't if you echo $result it gives you a resource id of 8 so there's something there.

#8 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 31 August 2006 - 04:56 PM

Thats resource holds the results from the query. It doesnt indicate whether there is any results or not. It sound like you query is failing. Echo your $sql variable:
echo $sql;
What does it return?

#9 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 31 August 2006 - 09:15 PM

A couple things with your code...

if ($num = '0'){

This line is using the assignment operator (=), instead of the comparison operator (==), which mean that when this line of code is executed, it is ALWAYS reassigning the variable $num to the string '0'.

This will cause the if() part of your conditional to always be TRUE, even if there were result rows returned by your query.

Change your code to look like this, and you should be good to go:

<?php

$sql = "SELECT cart_id, stock_id, qty, price  FROM t01_cart WHERE cart_id = '".GetCartId()."' ";

$result = mysql_query($sql, $dbLink)or die ('sql error'.mysql_error());

$num = mysql_num_rows($result);

		
if ($num == 0){
	echo 'Your basket is empty';
} else {
	echo $num;
}
		
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users