Mysq查询到Mysqli和Prepare Statement

时间:2014-05-03 14:02:56

标签: php mysql mysqli prepared-statement

我正在尝试将旧查询转换为mysqli + prepare语句,但我可以使用一些帮助。

我的旧循环看起来像这样:

$query = "SELECT * FROM `table`;";
$result = @mysql_query($query);

if (@mysql_num_rows($result) > 0) {
    for ($i=0; $i < @mysql_num_rows($result); $i++) {
        $row = @mysql_fetch_array($result);

        print $row['id'];
        print " - ";
        print $row['username'];
        print "<br>";
    }
}

但我坚持使用mysqli + prepare语句..

谢谢!

1 个答案:

答案 0 :(得分:-1)

$stmt = $mysqli->prepare("SELECT * FROM `table` WHERE `columnStr`= ? AND `columnInt`= ?");
$stmt->bind_param('si','string', 1);
$result = $stmt->execute();
while ($row = $result->fetch_row()) {
    var_dump($row);
}

字体:Mysqli Prepare