Solved

echo query in PHP

Posted on 2012-04-04
11
224 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 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 48
Selecting SUM data from mysql table with PHP 4 19
Testing a string condition in PHP 6 22
How can I implement a "Select All" with this configuration...? 6 39
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
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…
The viewer will learn how to count occurrences of each item in an array.

776 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