views:

193

answers:

3

Say, this is my query

$stmt = "SELECT * FROM foo WHERE x=?";

How do I print the above query plus the bound parameter to the screen or to a file in PHP?


When I print $stmt it says:

mysqli_stmt could not be converted to string

Here's my code:

if ($stmt = $this->conn->prepare($query)) { 
  $stmt ->bind_param('ss', $un, $pwd); 
  logToFile ("my.log", "inside mysql\n"); 
  logToFile ("my.log", $stmt); 
A: 

EDIT:

Quick Answer

Ah, I understand now. You are trying to print an array. You need to iterate/loop through it to print each value. $stmt returns from SQLite as an array.

Try this to start:

foreach( $stmt as $key => $value){
    echo "Key: $key, Value: $value <br />";
}

An explanation:

What happens here is that PHP receives an array from SQL and every element in an array has a key and a value. Consider the following:

$myArray[0] = "ElementZero";
$myArray[1] = "ElementOne";
$myArray[2] = "ElementTwo";
...
$myArray[15] = "ElementFifteen";

The numbers in the square brackets (0,1,2,15) are your keys and the text in quotes are your values. This is an indexed array, where each element has an index number. An associative array is where strings are used instead of numbers to identify each item in the array.A quick example of this:

$myArray['bob'] = "foo";
...
$myArray['joe'] = "bar";

In this array, we use strings as keys instead of numbers. Personally, I only see this useful when dealing with multidimensional arrays, but that is not for now...

Good luck, hope this helps.

Moshe
Moshe, that gives out an error.>Catchable fatal error: Object of class mysqli_stmt could not be converted to string in H:\wamp\www\magicmall\classes\Mysql.php on line 29
ekalaivan
+3  A: 

You can log your PHP variable $query which contains the SQL query with parameter placeholders.

But you probably want to log the query combined with the parameter values. This is a very common request, but this information is not exposed through the client library, so you can't read it from PHP.

You can get the query combined with the parameter values in the MySQL General Query Log, as you execute() the query.

Bill Karwin
A: 
print $query;

but don't you know what is in $query?

try:

print serialize($stmt);

it will tell you what is in the stmt object

Actually, serialize is my favorite debugging output format.

Don