Jump to content

mysql_numrows


mrmoz

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]

Link to comment
https://forums.phpfreaks.com/topic/19269-mysql_numrows/
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]
Link to comment
https://forums.phpfreaks.com/topic/19269-mysql_numrows/#findComment-83551
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]
Link to comment
https://forums.phpfreaks.com/topic/19269-mysql_numrows/#findComment-83565
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]
Link to comment
https://forums.phpfreaks.com/topic/19269-mysql_numrows/#findComment-83570
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());
Link to comment
https://forums.phpfreaks.com/topic/19269-mysql_numrows/#findComment-83585
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]
Link to comment
https://forums.phpfreaks.com/topic/19269-mysql_numrows/#findComment-83688
Share on other sites

Archived

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

×
×
  • 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.