reakt Posted June 15, 2011 Share Posted June 15, 2011 Hi Guys, this one is driving me mental. What have I done wrong? $itemId = '3806'; $sql4 = mysql_query("SELECT Recurring_Charges.serviceType, Recurring_Charges.description, Recurring_Charges.period, Recurring_Charges.amount, Service_Groups.name FROM Recurring_Charges, Service_Groups, Hosting_Services WHERE Hosting_Services.id = Recurring_Charges.serviceId AND Service_Groups.id = Hosting_Services.groupId AND Recurring_Charges.invoiceItemId ='$itemId' "); $num_rows = mysql_num_rows($sql4); echo "num_rows is $num_rows<br />\n"; while ($result4 = mysql_fetch_array($sql4)) { echo "got a result<br />\n"; print_r($result4); } Output is simply: num_rows is When I copy and paste the same query into phpmyadmin I get a proper result. Why doesn't it work and why don't I get an error or even 0 row count? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/239401-php-not-behaving-like-phpmyadmin/ Share on other sites More sharing options...
TOA Posted June 15, 2011 Share Posted June 15, 2011 Didn't read it carefully enough. Post removed by me Quote Link to comment https://forums.phpfreaks.com/topic/239401-php-not-behaving-like-phpmyadmin/#findComment-1230023 Share on other sites More sharing options...
cssfreakie Posted June 15, 2011 Share Posted June 15, 2011 well if it is exactly the same it should be al right. But do you have a connection with the database? Do you get any error? place this above your script error_reporting(E_ALL); ini_set("display_errors", 1); Quote Link to comment https://forums.phpfreaks.com/topic/239401-php-not-behaving-like-phpmyadmin/#findComment-1230130 Share on other sites More sharing options...
reakt Posted June 15, 2011 Author Share Posted June 15, 2011 I have a connection with the db, just didn't want to copy it in for security reasons. To prove the connection is ok, I added another query before the offending one, which works fine. I added your error reporting bit too and now I get a warning: $sql = mysql_query("SELECT * FROM Recurring_Charges"); $num_rows1 = mysql_num_rows($sql); echo "num_rows1 is $num_rows1<br />\n"; $itemId = '3806'; $sql4 = mysql_query("SELECT Recurring_Charges.serviceType, Recurring_Charges.description, Recurring_Charges.period, Recurring_Charges.amount, Service_Groups.name FROM Recurring_Charges, Service_Groups, Hosting_Services WHERE Hosting_Services.id = Recurring_Charges.serviceId AND Service_Groups.id = Hosting_Services.groupId AND Recurring_Charges.invoiceItemId = '$itemId'"); $num_rows = mysql_num_rows($sql4); echo "num_rows is $num_rows<br />\n"; while ($result4 = mysql_fetch_array($sql4)) { echo "got a result<br />\n"; print_r($result4); } Returns: num_rows1 is 2426 Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /var/websites/something/www/test.php on line 29 num_rows is Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /var/websites/something/www/test.php on line 31 The variable it's referring to is obviously $sql4, but why does it think it's a boolean? As stated when I copy and paste the exact statement into phpmyadmin and change $itemId to 3806 I get a proper result containing 1 row. Quote Link to comment https://forums.phpfreaks.com/topic/239401-php-not-behaving-like-phpmyadmin/#findComment-1230234 Share on other sites More sharing options...
reakt Posted June 15, 2011 Author Share Posted June 15, 2011 I apologise for wasting your time guys. I've just realized I was running my php script on one mysql installation, and testing the query in phpmyadmin on another installation. They are supposed to be the same, but I just found the table Service_Groups doesn't exist on the dev installation. Once again, sorry! Quote Link to comment https://forums.phpfreaks.com/topic/239401-php-not-behaving-like-phpmyadmin/#findComment-1230239 Share on other sites More sharing options...
cssfreakie Posted June 16, 2011 Share Posted June 16, 2011 well at least you got an error please mark the topic solved saves others time reading through Quote Link to comment https://forums.phpfreaks.com/topic/239401-php-not-behaving-like-phpmyadmin/#findComment-1230251 Share on other sites More sharing options...
PFMaBiSmAd Posted June 16, 2011 Share Posted June 16, 2011 Having some error checking and error reporting logic testing the result of the mysql_query() statement and using mysql_error() in the error reporting logic would have quickly pointed to the problem. Quote Link to comment https://forums.phpfreaks.com/topic/239401-php-not-behaving-like-phpmyadmin/#findComment-1230255 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.