Jump to content

Archived

This topic is now archived and is closed to further replies.

mrmoz

mysql_numrows

Recommended Posts

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

  [code] $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() );[/code]

Share this post


Link to post
Share on other sites
cheek the query ok not the result?

good luck

[code]
<?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";

}
?>
[/code]

Share this post


Link to post
Share on other sites
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:

[code]
<? 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;} ?>[/code]

Share this post


Link to post
Share on other sites
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.
[code=php:0]<? 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;
} ?>[/code]

Share this post


Link to post
Share on other sites
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());

Share this post


Link to post
Share on other sites
Swap:
[code]
if (mysql_num_rows($result) == 0)[/code]

With this:
[code]$num = mysql_num_rows($result);
if($num == 0)[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:
[code]echo $sql;[/code]
What does it return?

Share this post


Link to post
Share on other sites
A couple things with your code...

[code=php:0]
if ($num = '0'){
[/code]

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

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

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

[code]
<?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;
}

?>
[/code]

Share this post


Link to post
Share on other sites

×

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.