jeff5656 Posted October 26, 2009 Share Posted October 26, 2009 I get a query empty error with this. I do a while loop for all the servicesa, and then inside that I query another table to match the service of the first query. Hope that is not too confusing and that the code will better show what I'm trying to do: $month = date('m'); $year= date("Y"); include ("../connectdb.php"); $query = "SELECT * FROM `services` order by service "; $results = mysql_query ($query2) or die (mysql_error()); while ($row = mysql_fetch_assoc ($results)) { $service=$row['service']; echo "service is".$service; ?><th width="8%"><?php echo ucfirst($service);?></th> </tr><?php $query2 = "SELECT * FROM callsched WHERE service = '$service' AND MONTH(calldate)=$month AND YEAR(calldate)=$year ORDER BY calldate "; $results2 = mysql_query ($query2) or die (mysql_error()); while ($row2 = mysql_fetch_assoc ($results2)) { ?> <tr> <td bgcolor="#FFFFFF"><?php echo $row2['calldate'];?></td> </tr> <?php } }?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/179060-solved-query-is-empty/ Share on other sites More sharing options...
simshaun Posted October 26, 2009 Share Posted October 26, 2009 I've never seen a "query empty" error. Are you referring to MySQL throwing an actual error or are you saying no records are returned. Also, if you are running a query inside of a loop you are usually taking the wrong approach. Those two queries could be easily combined in to a single query using joins. Edit: SQL would be something along the lines of SELECT list ,out ,only ,the ,fields ,you ,need ,not ,* FROM services svc INNER JOIN callsched call ON call.service = svc.service AND MONTH(calldate) = MONTH() AND YEAR(calldate) = YEAR() ORDER BY svc.service ,call.calldate Quote Link to comment https://forums.phpfreaks.com/topic/179060-solved-query-is-empty/#findComment-944719 Share on other sites More sharing options...
PFMaBiSmAd Posted October 26, 2009 Share Posted October 26, 2009 The "empty query" error is because the variable you put into the first mysql_query() is NOT the variable you put the sql statement into. I wish you were developing and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini to get php to help you. There would have been an undefined error message concerning the mismatch in variable names at the first mysql_query() statement. Quote Link to comment https://forums.phpfreaks.com/topic/179060-solved-query-is-empty/#findComment-944780 Share on other sites More sharing options...
jeff5656 Posted October 26, 2009 Author Share Posted October 26, 2009 The "empty query" error is because the variable you put into the first mysql_query() is NOT the variable you put the sql statement into. Which variable are you talking about? $service? I thought I defined service to be $row['service'] inside the first while loops, and then in the second while loop I query the table WHERE service is equal to $service. Quote Link to comment https://forums.phpfreaks.com/topic/179060-solved-query-is-empty/#findComment-944790 Share on other sites More sharing options...
mikesta707 Posted October 26, 2009 Share Posted October 26, 2009 $query = "SELECT * FROM `services` order by service "; $results = mysql_query ($query2) or die (mysql_error()); notice something wrong with this? Quote Link to comment https://forums.phpfreaks.com/topic/179060-solved-query-is-empty/#findComment-944791 Share on other sites More sharing options...
jeff5656 Posted October 26, 2009 Author Share Posted October 26, 2009 As for the response about JOIN, I don't think this would solve my problem. I want to query the first table that has a field name called "service". For each service, I want to query a second table and select records that match *each* service from first table, one at a time. I seems I need a query inside a query (i.e. two while loops) so I don't know how joining the two tables togetehr would solve that (I am too new at PHP to figure that out :-) ) Quote Link to comment https://forums.phpfreaks.com/topic/179060-solved-query-is-empty/#findComment-944793 Share on other sites More sharing options...
jeff5656 Posted October 26, 2009 Author Share Posted October 26, 2009 $query = "SELECT * FROM `services` order by service "; $results = mysql_query ($query2) or die (mysql_error()); notice something wrong with this? oh jeez....ok query2. Let me go try this. thatnk you! Quote Link to comment https://forums.phpfreaks.com/topic/179060-solved-query-is-empty/#findComment-944794 Share on other sites More sharing options...
mikesta707 Posted October 26, 2009 Share Posted October 26, 2009 BTW I believe join would do exactly what you want. I'm not a SQL guru or anything, but I am pretty sure JOIN can be used instead of doing queries in a while Quote Link to comment https://forums.phpfreaks.com/topic/179060-solved-query-is-empty/#findComment-944803 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.