且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Mysqli查询仅适用于本地主机,不适用于Web服务器

更新时间:2023-02-23 15:38:30

每次调用mysqli/stmt方法都可能失败.您应该检查每一个.
尝试使用error_reporting(E_ALL),也许display_error = On

Each call to a mysqli/stmt method can fail. You should check each and every one.
Try it with error_reporting(E_ALL) and maybe display_error=On

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);

// passing database name as fourth parameter
$mysqli = new mysqli("server", "user", "password", 'dbname');
if (mysqli_connect_errno()) {
   printf("Can't connect Errorcode: %s\n", mysqli_connect_error());
   exit;
}

// Query used  
$query = "SELECT name FROM users WHERE id = ?";

if ( false===($stmt=$mysqli->prepare("$query")) ) {
  echo 'mysqli::prepare failed: ', htmlspecialchars($mysqli->error);
  die;
}

// Specify parameters to replace '?'
$rc = $stmt->bind_param("d", $id);  
if ( !$rc ) {
  echo 'bind_param failed: ', htmlspecialchars($stmt->error);
  die;
}

echo '<pre>Debug: execute()</pre>';
$rc = $stmt->execute();
if ( !$rc ) {
  echo 'execute failed: ', htmlspecialchars($stmt->error);
  die;
}

echo '<pre>Debug: bind_result()</pre>';
// bind variables to prepared statement 
$rc = $stmt->bind_result($_userName);
if ( !$rc ) {
  echo 'bind_result failed: ', htmlspecialchars($stmt->error);
  die;
}

echo '<pre>Debug: fetch()</pre>';
while ($stmt->fetch()) 
{
  echo 'username: ', $_userName;
}
echo '<pre>Debug: stmt close()</pre>';
$stmt->close();

echo '<pre>Debug: mysqli close()</pre>';
$mysqli->close();