mrmoz Posted August 31, 2006 Share Posted August 31, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/19269-mysql_numrows/ Share on other sites More sharing options...
redarrow Posted August 31, 2006 Share Posted August 31, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/19269-mysql_numrows/#findComment-83551 Share on other sites More sharing options...
mrmoz Posted August 31, 2006 Author Share Posted August 31, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/19269-mysql_numrows/#findComment-83565 Share on other sites More sharing options...
wildteen88 Posted August 31, 2006 Share Posted August 31, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/19269-mysql_numrows/#findComment-83570 Share on other sites More sharing options...
mrmoz Posted August 31, 2006 Author Share Posted August 31, 2006 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()); Quote Link to comment https://forums.phpfreaks.com/topic/19269-mysql_numrows/#findComment-83585 Share on other sites More sharing options...
wildteen88 Posted August 31, 2006 Share Posted August 31, 2006 Swap:[code]if (mysql_num_rows($result) == 0)[/code]With this:[code]$num = mysql_num_rows($result);if($num == 0)[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19269-mysql_numrows/#findComment-83587 Share on other sites More sharing options...
mrmoz Posted August 31, 2006 Author Share Posted August 31, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/19269-mysql_numrows/#findComment-83590 Share on other sites More sharing options...
wildteen88 Posted August 31, 2006 Share Posted August 31, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/19269-mysql_numrows/#findComment-83592 Share on other sites More sharing options...
HeyRay2 Posted August 31, 2006 Share Posted August 31, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/19269-mysql_numrows/#findComment-83688 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.