MySQLi在数组中准备语句查询结果

And*_*Dev -1 php arrays mysqli prepared-statement

试图将我的旧mysql查询转换为mysqli预处理语句.除了一件事,我已经弄明白了.如何将查询结果存储为数组?我以前这样做:

$sql = "SELECT * FROM Users";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result) {
 // do stuff
}
Run Code Online (Sandbox Code Playgroud)

现在我有以下代码.在这种情况下,我的数组是一个单独的记录,所以我不需要迭代它,但我想把它作为一个数组保存,以便我可以引用它的字段名称.此外,我将有其他查询将返回多个记录,所以我需要iterat然后.

$sql = "SELECT * FROM Users 
    WHERE (LOWER(first_name)=LOWER(?) && LOWER(last_name)=LOWER(?))";
$stmt =  mysqli_stmt_init($link);
$this_user;

if (mysqli_stmt_prepare($stmt, $sql)) {

    /* Bind the input parameters to the query */
    mysqli_stmt_bind_param($stmt, 'ss', $first_name, $last_name);

    /* execute query, store results in an array */
    mysqli_stmt_execute($stmt);
    $result = mysqli_fetch_array($stmt);

    if (mysqli_num_rows($result) == 0) {
        mysqli_stmt_close($stmt);
        mysqli_close($link);
        $tag_result = "failure";
        $tag_message = "No matching user found";
        echo encodeJSONObj($tag_result, $tag_message);
        die();
    }

    if (mysqli_num_rows($result) > 1) {
        mysqli_close($link);
        $tag_result = "failure";
        $tag_message = "Multiple records found for this user.";
        echo encodeJSONObj($tag_result, $tag_message);
        die();
    }

    $this_user = mysqli_fetch_array($result);

    /* close statement */
    mysqli_stmt_close($stmt);
}

$id                     = $this_user['id'];
$first_name             = $this_user['first_name'];
$last_name              = $this_user['last_name'];
// and so on...
Run Code Online (Sandbox Code Playgroud)

有人能告诉我我做错了什么吗?谢谢!

编辑:非常感谢Phil,我修改了我的代码.但是,即使我的输入参数应该返回正好1行,我仍然似乎返回0行.这是我有的:

$sql = "SELECT id, first_name, last_name, group_id, email, cell
            FROM Users 
            WHERE (first_name=? && last_name=?)";
$stmt =  mysqli_stmt_init($link);

if (mysqli_stmt_prepare($stmt, $sql)) {

    /* Bind the input parameters to the query */
    mysqli_stmt_bind_param($stmt, 'ss', $first_name, $last_name);

    /* execute query, bind result, and fetch value */
    mysqli_stmt_execute($stmt);
    mysqli_stmt_bind_result($stmt, $id, $first_name, $last_name,  $group_id, $email, $cell);
    mysqli_stmt_fetch($stmt);

    if (mysqli_stmt_num_rows($stmt) == 0) {
        mysqli_stmt_close($stmt);
        mysqli_close($link);
        echo "No results returned";
        die();
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

No results returned当它应该找到1行并向右跳过该块时,它总是输出.我已经盯着这个很长一段时间,但我看不出我做错了什么.

Phi*_*hil 5

您的脚本包含许多错误(如上面的评论中所述).这是一个简单的步骤......

  1. 准备一个语句并绑定参数

    $stmt = $link->prepare($sql);
    if (!$stmt) {
        throw new Exception($link->error, $link->errno);
    }
    
    // you can error check this too but it rarely goes wrong
    $stmt->bind_param('ss', $first_name, $last_name);
    
    Run Code Online (Sandbox Code Playgroud)
  2. 执行语句并存储结果

    if (!$stmt->execute()) {
        throw new Exception($stmt->error, $stmt->errno);
    }
    $stmt->store_result();
    
    Run Code Online (Sandbox Code Playgroud)
  3. 对你的行检查数量是多少$stmt->num_rows ...

    if ($stmt->num_rows == 0) {
        // ... 
    }
    if ($stmt->num_rows > 1) {
        // ...
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 绑定并获取结果

    // This relies on the SELECT column ordering.
    // You should probably change your SELECT statement to
    // SELECT id, first_name, last_name FROM Users...
    $stmt->bind_result($id, $first_name, $last_name);
    $stmt->fetch();
    $stmt->close();
    $link->close();
    
    Run Code Online (Sandbox Code Playgroud)

    如果要将单个结果行作为关联数组获取,请尝试使用此方法

    $result = $stmt->get_result(); // note - this requires the mysqlnd driver
    $this_user = $result->fetch_array(MYSQLI_ASSOC);
    $result->free();
    $stmt->close();
    $link->close();
    
    Run Code Online (Sandbox Code Playgroud)