Solved

echo query in PHP

Posted on 2012-04-04
11
220 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
  • 6
  • 5
11 Comments
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
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
Comment Utility
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 30

Expert Comment

by:Marco Gasi
Comment Utility
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
 

Author Comment

by:digitalwise
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Assisted Solution

by:Marco Gasi
Marco Gasi earned 500 total points
Comment Utility
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 30

Expert Comment

by:Marco Gasi
Comment Utility
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
Comment Utility
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 30

Accepted Solution

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

Author Closing Comment

by:digitalwise
Comment Utility
Thanks!
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
I'm glad to help you. Bye.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now