PHP mysqli->准备什么都不返回

时间:2015-02-08 17:54:21

标签: php mysql database mysqli

我正在尝试使用mysqli连接从我的数据库中检索行,但无论我尝试什么,我都会继续收到500内部服务器错误。

$getUserQuery = "SELECT * FROM members WHERE name = ? AND member_id > 0";
$getUserStatement = $mysqli_conn->prepare($getUserQuery);
$getUserStatement->bind_param("s", $name);

$ mysqli_conn-> ping()导致值为true,因此我知道数据库连接没有问题。 var_dump($ getUserStatement)会导致bool(false),因此准备工作存在一些问题。整个代码:

$user_dirty = $_GET['u'];
$pass_dirty = $_GET['p'];

$getUserQuery = "SELECT * FROM members WHERE name = ? AND member_id > 0";
$getUserStatement = $mysqli_conn->prepare($getUserQuery);

if ($getUserStatement) {
    echo($getUserStatement);
} else {
    echo("not good");
}

$getUserStatement->bind_param("s", $user_dirty);
$getUserStatement->execute();
$getUserResult = $getUserStatement->get_result();

以及我如何创建数据库连接:

$mysql_host = "host";
$mysql_user = "user";
$mysql_password = "pass";
$mysql_database = "db";

$mysqli_conn = new mysqli($mysql_host, $mysql_user, $mysql_password, $mysql_database);

1 个答案:

答案 0 :(得分:0)

你得到500个内部错误,因为语句可能是假的(https://stackoverflow.com/a/845025/2962442打开显示错误),不是它应该的资源,它可能是由于连接丢失,尝试ping或重新连接准备之前。

示例好代码:

$getUserQuery = "SELECT * FROM members WHERE name = ? AND member_id > 0";
$getUserStatement = $mysqli_conn->prepare($getUserQuery);
if ($getUserStatement) {
    $getUserStatement->bind_param("s", $name);
} else {
    //statement is false, not good
}

和连接部分的好例子

$mysql_host = "host";
$mysql_user = "user";
$mysql_password = "pass";
$mysql_database = "db";

$mysqli_conn = new mysqli($mysql_host, $mysql_user, $mysql_password, $mysql_database);    
if (!$mysqli_conn) {
    //not connected...
}