我不知道到底发生了什么,但是当我上传网站时,我的所有列都只返回了第一个字符。它在本地计算机上工作得很好。
我在这里发现了一个类似的问题,但我没有找到答案:
https://stackoverflow.com/questions/10507848/mysql-query-returns-only-the-first-letter-of-strings-only-when-page-is-viewed-on
// Log Table Query
unset($stmt);
$stmt = $db->stmt_init();
$stmt = $db->prepare( "SELECT * FROM logs ORDER BY `id` DESC" );
$stmt->store_result();
$stmt->bind_result($r_id, $r_time, $r_logger, $r_message, $r_category);
$stmt->execute();
while( $stmt->fetch() )
{
var_dump($r_message);
var_dump($r_category);
}
$stmt->close();
这在本地主机上输出,例如:
字符串(5) "你好"字符串(3) "牛"
但在实时服务器上:
字符串(1) "H"字符串(1) "C"
有什么想法吗?
编辑
我认为这仅适用于字符串类型。整数类型返回例如:
国际(2893)
我假设您的数据库或表配置与您的本地主机相似(最好仔细检查您的表)。我注意到一个错误:
1.您在打电话之前store_result()
打电话给execute()
。按照 http://php.net/manual/en/mysqli-stmt.store-result.php execute()
应该首先调用。
请参阅我的代码,这可能会解决您的问题:
/* unsetting doesn't matter you're
going to overwrite it anyway */
unset($stmt);
/* you dont need to initialize $stmt with $db->stmt_init(),
$db->prepare() method will create it for you */
$stmt = $db->stmt_init();
$stmt = $db->prepare("SELECT * FROM logs ORDER BY `id` DESC");
/* execute the query first before storing
the result and binding it your variables */
if (!$stmt->execute()) {
echo "query execution error";
exit();
}
/* store the result */
$stmt->store_result();
/* then bind your variables */
$stmt->bind_result($r_id, $r_time, $r_logger, $r_message, $r_category);
/* fetch data and display */
while($stmt->fetch()) {
var_dump($r_message);
var_dump($r_category);
}
$stmt->close();
让我知道这是否解决了您的问题。
或者,您可以使用直接的方式,因为您没有为查询提供任何输入参数,例如WHERE first_name LIKE <input here>
:
$result = $db->query("SELECT * FROM logs ORDER BY `id` DESC");
if ($result === false) {
echo "query execution error";
exit();
}
/* You can use either MYSQLI_NUM or MYSQLI_ASSOC os MYSQLI_BOTH
see php.net for more info */
echo "<pre>";
while($line = $result->fetch_array(MYSQLI_NUM)) {
print_r($line);
echo "'n";
}
echo "</pre>";
如您链接的问题中所建议的,尝试不带unset($stmt)
的代码,$stmt = db->stmt_init()
相反,您可以使用free_result()
// Log Table Query
$stmt->free_result(); //Free old results first
$stmt = $db->prepare( "SELECT * FROM logs ORDER BY `id` DESC" );
$stmt->store_result();
$stmt->bind_result($r_id, $r_time, $r_logger, $r_message, $r_category);
$stmt->execute();
while( $stmt->fetch() )
{
var_dump($r_message);
var_dump($r_category);
}
$stmt->close();