Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

echo query in PHP

Posted on 2012-04-04
11
Medium Priority
?
242 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 2000 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

704 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