编写MySQLi语句

时间:2013-02-09 19:18:15

标签: php mysqli

我一直使用PDO语句,但出于某种原因,我无法说服服务器人为php安装PDO,但我确实有MySQLi,我不知道我做错了什么,我不明白连接错误,无论我如何输出一个错误,我都不会收到查询错误。这就是我正在做的事情。

include 'MySQLiConnect.php';

if($stmt = $mysqli->prepare("SELECT * FROM zipCodeTable WHERE zip_code = ?")){

    $stmt->bind_param("s", '07110');

    $stmt->execute();

    $stmt->bind_result($resultsArray);

    $stmt->fetch();

    foreach($resultsArray as $columnData){

        $matchingZipcode = $columnData['zip_code'];
        $matchingTimezone = $columnData['time_zone'];
    }       

    $stmt->close();


}

echo $matchingZipcode.', '.$matchingTimezone;

这基本上只是为了确认一个用户的邮政编码,以前从未使用过MySQLi编写的语句,我试着直接从手册中做到,不知道我做错了什么。感谢您抽出宝贵时间阅读本文。

1 个答案:

答案 0 :(得分:3)

您正在尝试“绑定”文字字符串。你不能这样做。您必须绑定变量。

更改

$stmt->bind_param("s", '07110');

$string = '07110';

$stmt->bind_param("s", $string);

此外,绑定结果时,必须为返回的每个字段提供变量。

例如:

$stmt->bind_result($zipCode, $timeZone);

使用SELECT *时,这有点问题。您可能有兴趣查看此评论,了解您可能希望如何处理它:http://www.php.net/manual/en/mysqli-stmt.bind-result.php#85470