Mysql query to Mysqli and Prepare Statement

2019-09-01 01:10发布

I am trying to convert my old queries to mysqli + prepare statement but I could use some help.

My old loop looks something like this:

$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>";
    }
}

But I am stuck with the mysqli + prepare statement..

Thanks!

1条回答
放荡不羁爱自由
2楼-- · 2019-09-01 01:52
$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);
}

Font: Mysqli Prepare

查看更多
登录 后发表回答