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

时间:2014-02-18 23:01:49

标签: php arrays mysqli prepared-statement

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

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

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

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

编辑:非常感谢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();
    }

    ...
}

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

1 个答案:

答案 0 :(得分: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);
    
  2. 执行语句并存储结果

    if (!$stmt->execute()) {
        throw new Exception($stmt->error, $stmt->errno);
    }
    $stmt->store_result();
    
  3. 对<{1}}进行检查...

    $stmt->num_rows
  4. 绑定并获取结果

    if ($stmt->num_rows == 0) {
        // ... 
    }
    if ($stmt->num_rows > 1) {
        // ...
    }
    

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

    // 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();
    
相关问题