当使用where子句时,mysqli执行不返回结果

时间:2019-04-09 06:27:05

标签: php mysqli

嗨,我有一个参数化查询,我正在尝试执行并从中获取最佳结果。当我有我的user_id where子句时,尽管第一行匹配,但没有返回任何结果。我的语法某种程度上是错误的,但是我看不出问题出在哪里。

因此返回结果:

$stmt = mysqli_stmt_init($conn);
$stmt = $conn->prepare("SELECT member_id, company_name FROM member");

$execute_result = $stmt->execute();
if (!$execute_result) 
{
    throw new Exception("Execute failed: (" . $stmt->errno . ") " . $stmt->error);
}

$stmt->bind_result($col_member_id, $col_company_name);

if ($stmt->fetch())
{
    $_SESSION['member_id'] = $col_member_id;
    $_SESSION['company_name'] = $col_company_name;
}
$stmt->close();

但这不是:

$stmt = mysqli_stmt_init($conn);
$stmt = $conn->prepare("SELECT member_id, company_name FROM member WHERE userid=?");
$bind_result = $stmt->bind_param("s", $userid);

$execute_result = $stmt->execute();
if (!$execute_result) 
{
    throw new Exception("Execute failed: (" . $stmt->errno . ") " . $stmt->error);
}

$stmt->bind_result($col_member_id, $col_company_name);

if ($stmt->fetch())
{
    ## Doesn't come in here for some reason
    $_SESSION['member_id'] = $col_member_id;
    $_SESSION['company_name'] = $col_company_name;
}
$stmt->close();

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

按照@NigelRen的答案,查询是正确的,但输入值不是预期的值。