How do I check what a query is doing?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do I check what a query is doing?

Post by simonmlewis »

Code: Select all

include "dbconn.php";
$result = mysql_query ("SELECT id, romancode, players FROM events WHERE romancode = '$itemcode'") or die(mysql_error());
This is producing nothing, yet it should be a very basic query. I've tested and $itemcode has something in it.

So how do I 'print' what $result is producing - I've totally forgotten.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I check what a query is doing?

Post by Celauran »

You mean var_dump? I'd also move the query itself into a variable and echo that.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I check what a query is doing?

Post by simonmlewis »

Hi. I need some form of example please.
However, the root issue here has been resolved. It seems that there was hidden code in the variable, which I discovered and have since stripped out.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How do I check what a query is doing?

Post by social_experiment »

Code: Select all

<?php
$result = mysql_query ("SELECT id, romancode, players FROM events WHERE romancode = '$itemcode'");
var_dump($result);
?>
Depending on what the return result is you can figure out whether the query failed or not (in case of a SELECT statement, mysql_query() will return a resource on success and false on failure.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Jessica159
Forum Newbie
Posts: 8
Joined: Mon Mar 11, 2013 2:35 am

Re: How do I check what a query is doing?

Post by Jessica159 »

Dear, visit this page http://stackoverflow.com/questions/1532 ... ted-in-pdo... hopefully it will be useful for you.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do I check what a query is doing?

Post by requinix »

Jessica159 wrote:Dear, visit this page http://stackoverflow.com/questions/1532 ... ted-in-pdo... hopefully it will be useful for you.
Since OP isn't using PDO (though it's a good idea to switch to it) I wouldn't hold my breath.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I check what a query is doing?

Post by simonmlewis »

I get this:
bool(true)

I'm doing an INSERT query now. But the data isn't going in.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How do I check what a query is doing?

Post by social_experiment »

Celauran wrote:move the query itself into a variable and echo that.
what output do you receive when you echo the query?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do I check what a query is doing?

Post by Christopher »

Do you mean how do you return the result set?

Code: Select all

include "dbconn.php";
$error = '';
$rows = array();
$result = mysql_query ("SELECT id, romancode, players FROM events WHERE romancode = '$itemcode'");
if (!mysql_errno($result)) {
  while ($row = mysql_fetch_assoc($result)) {
    $rows[] = $row;
  }
} else {
  $error = mysql_error($result);
}
echo "error=$error<br/>";
echo 'rows=<pre>' . print_r($rows, 1) . '</pre>';
(#10850)
Post Reply