Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/179060-solved-query-is-empty/
Share on other sites

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

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.

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.

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 :-) )

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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