如何使用预准备语句检索关联数组?

时间:2014-01-07 07:03:21

标签: php mysqli

在关于获取关联数组(http://php.net/manual/en/mysqli-result.fetch-assoc.php)的php文档中,它没有给出任何使用预准备语句的示例。我试着重新解释那里给出的代码来使用我过去做过的准备好的语句,这些语句确实有效。但这段代码只是错误。是否有可能为此使用准备好的陈述?如果是这样,怎么样?

    <?php
    $mysqli = new mysqli('website','username','pw','db');

    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

    $startTime = 1389056400;
    $query = $mysqli->prepare("SELECT * FROM Table2 WHERE Start = ?");
    $query->bind_param('i', $startTime);
    $result = $mysqli->query($query);

    /* associative array */
    $row = $result->fetch_array(MYSQLI_ASSOC);
    echo $row["Start"];
    echo $row["End"];

    /* free result set */
    $result->free();

    /* close connection */
    $mysqli->close();
    ?>

1 个答案:

答案 0 :(得分:1)

您的查询是一个对象而不是字符串,查询函数需要一个字符串。

你不应该像这样调用查询函数,你应该调用execute。

尝试:

$result = $query->execute();

从这里开始:http://www.php.net/manual/en/mysqli.prepare.php

相关问题