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?
如何将上述查询加上绑定参数打印到屏幕或PHP中的文件?
When I print $stmt
it says:
当我打印$ stmt时,它说:
mysqli_stmt could not be converted to string
mysqli_stmt无法转换为字符串
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);
3 个解决方案
#1
3
You can log your PHP variable $query
which contains the SQL query with parameter placeholders.
您可以使用参数占位符记录包含SQL查询的PHP变量$ query。
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.
但您可能希望将查询与参数值结合使用。这是一个非常常见的请求,但此信息不会通过客户端库公开,因此您无法从PHP中读取它。
You can get the query combined with the parameter values in the MySQL General Query Log, as you execute()
the query.
在执行()查询时,您可以将查询与MySQL常规查询日志中的参数值结合使用。
#2
0
print $query;
but don't you know what is in $query?
但是你不知道$ query中的内容是什么吗?
try:
print serialize($stmt);
it will tell you what is in the stmt object
它会告诉你stmt对象中的内容
Actually, serialize is my favorite debugging output format.
实际上,serialize是我最喜欢的调试输出格式。
#3
0
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.
啊,我现在明白了。您正在尝试打印阵列。您需要迭代/循环它以打印每个值。 $ stmt从SQLite作为数组返回。
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:
这里发生的是PHP从SQL接收数组,并且数组中的每个元素都有一个键和一个值。考虑以下:
$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:
方括号(0,1,2,15)中的数字是您的键,引号中的文本是您的值。这是一个索引数组,其中每个元素都有一个索引号。关联数组是使用字符串而不是数字来标识数组中的每个项目。一个简单的示例:
$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.
祝你好运,希望这会有帮助。
#1
3
You can log your PHP variable $query
which contains the SQL query with parameter placeholders.
您可以使用参数占位符记录包含SQL查询的PHP变量$ query。
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.
但您可能希望将查询与参数值结合使用。这是一个非常常见的请求,但此信息不会通过客户端库公开,因此您无法从PHP中读取它。
You can get the query combined with the parameter values in the MySQL General Query Log, as you execute()
the query.
在执行()查询时,您可以将查询与MySQL常规查询日志中的参数值结合使用。
#2
0
print $query;
but don't you know what is in $query?
但是你不知道$ query中的内容是什么吗?
try:
print serialize($stmt);
it will tell you what is in the stmt object
它会告诉你stmt对象中的内容
Actually, serialize is my favorite debugging output format.
实际上,serialize是我最喜欢的调试输出格式。
#3
0
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.
啊,我现在明白了。您正在尝试打印阵列。您需要迭代/循环它以打印每个值。 $ stmt从SQLite作为数组返回。
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:
这里发生的是PHP从SQL接收数组,并且数组中的每个元素都有一个键和一个值。考虑以下:
$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:
方括号(0,1,2,15)中的数字是您的键,引号中的文本是您的值。这是一个索引数组,其中每个元素都有一个索引号。关联数组是使用字符串而不是数字来标识数组中的每个项目。一个简单的示例:
$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.
祝你好运,希望这会有帮助。