Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

echo query in PHP

Is there is a way in PHP to output what query is actually being run?  I have
$getcount = mysql_query("SELECT count(RegID) as CountMe FROM Registrations WHERE CourseID = " .$row['ClassID'] . "and DateProcessed is not null");

Open in new window

which is not returning any results but it should be so I wanted to see what was actually being run.   I know how to in ASP and CF.    Thanks!
0
digitalwise
Asked:
digitalwise
  • 6
  • 5
2 Solutions
 
Marco GasiFreelancerCommented:
Do this:

$getcount = mysql_query("SELECT count(RegID) as CountMe FROM Registrations WHERE CourseID = " .$row['ClassID'] . "and DateProcessed is not null") or die(mysql_error() . "The query was:<br>" . $getcount;

Cheers
0
 
digitalwiseAuthor Commented:
You have a syntax error - missing ) after the die statement.    But other than that - nothing gets returned - I don't have The query was: or anything else outputting...
0
 
Marco GasiFreelancerCommented:
Sorry for the typo. But I did a bad error try this:

$sql = "SELECT count(RegID) as CountMe FROM Registrations WHERE CourseID = " .$row['ClassID'] . "and DateProcessed is not null";
$getcount = mysql_query($sql) or die(mysql_error() . "The query was:<br>" . $sql);
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
digitalwiseAuthor Commented:
OK - that worked only in the sense that I had an issue in the SQL (missing space) but if the query doesn't have errors - nothing outputs.    So is that not possible in PHP?
0
 
digitalwiseAuthor Commented:
Maybe this will help.   This is what I am trying to solve.   Nothing is outputting in the table for $getcount.CountMe or $getcount2.CountMe.  What am I doing wrong?  

 
<?php
		while($row = mysql_fetch_array($getusers)){
			
			
			
		$getcount = mysql_query("SELECT count(RegID) as CountMe FROM Registrations WHERE CourseID = " .$row['ClassID'] . " and DateProcessed is not null");		
		$getcount2 = mysql_query("SELECT count(RegID) as CountMe FROM Registrations WHERE CourseID = " .$row['ClassID'] . " and DateProcessed is null");
		$finalcount = $row['SpotsAvailable'] - $getcount['CountMe'] - $getcount2['CountMe'];
			
echo "<tr><td valign='top'>";
echo "<A HREF='classes_edit.php?ClassID=" .$row['ClassID'] ."'>" .$row['ClassName'] . "</A>";
echo "</td><td valign='top'>";
echo $row['ClassLocation'];
echo "</td><td valign='top'>";
echo date("m-d-Y",strtotime($row['StartDate']));
echo "</td><td valign='top' align='center'>";
echo $row['SpotsAvailable'];
echo "</td><td valign='top' align='center'>";
echo $getcount['CountMe'];
echo "</td><td valign='top' align='center'>";
echo $getcount2['CountMe'];
echo "</td><td valign='top' align='center'>";
echo $finalcount;
echo "</td></tr>";
} 
	 ?>

Open in new window

0
 
Marco GasiFreelancerCommented:
You simply echo the $sql.

echo $sql;

The important thing is that you fist define your query in a variable like $sql or $query, then you can print it when and where you want.

Cheers
0
 
Marco GasiFreelancerCommented:
I think with your query is the pronblem in missing quotes around the $row['ClassID'] value. In addition, I see you don't have a space before the 'AND'. Try this:

$sql = "SELECT count(RegID) as CountMe FROM Registrations WHERE CourseID = '" .$row['ClassID'] . "' and DateProcessed is not null";
0
 
digitalwiseAuthor Commented:
OK - I think this is some sort of output error at this point.    The queries are working and they return the correct value at the direct SQL request.     Why don't I get an output.  
echo $getcount2['CountMe'];

Open in new window

0
 
Marco GasiFreelancerCommented:
You have to fetch $getcount:
$result = mysql_fetch_assoc($getcount);
echo $result['CountMe'];
0
 
digitalwiseAuthor Commented:
Thanks!
0
 
Marco GasiFreelancerCommented:
I'm glad to help you. Bye.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now