Solved

echo query in PHP

Posted on 2012-04-04
11
233 Views
Last Modified: 2012-04-04
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
Comment
Question by:digitalwise
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 37806070
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
 

Author Comment

by:digitalwise
ID: 37806095
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
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 37806112
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:digitalwise
ID: 37806136
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
 

Author Comment

by:digitalwise
ID: 37806150
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
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 500 total points
ID: 37806152
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
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 37806167
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
 

Author Comment

by:digitalwise
ID: 37806211
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
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 37806267
You have to fetch $getcount:
$result = mysql_fetch_assoc($getcount);
echo $result['CountMe'];
0
 

Author Closing Comment

by:digitalwise
ID: 37806463
Thanks!
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 37806471
I'm glad to help you. Bye.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question