将mysql函数更新为mysqli

时间:2015-05-10 09:24:42

标签: php mysql mysqli

我想将此代码更新为mysqli,但不知道从哪里开始。 我知道连接是这样处理的,但就iv而言。

$connection = mysqli_connect('localhost', 'admin', 'admin', 'database_name');  

要更新的功能:

$cn=mysql_connect('localhost', 'admin', 'admin') or die(mysql_error());
    mysql_select_db('database_name',$cn) or die(mysql_error());
    $sql = "SELECT name FROM category";
    $rs = mysql_query($sql) or die(mysql_error());
    echo "<select>";
    while($row = mysql_fetch_array($rs)){
    echo "<option value='".$row["name"]."'>".$row["name"]."</option>";
    }mysql_free_result($rs);
    echo "</select>";
<?php
$sql = mysqli_query($connection, 'SELECT name FROM category');
echo "<select>";
while($row = mysqli_fetch_array($sql)){
echo "<option value='".$row["name"]."'>".$row["name"]."</option>";
}mysql_free_result($sql);
echo "</select>";
?>

1 个答案:

答案 0 :(得分:0)

$cn=mysqli_connect('localhost', 'admin', 'admin','database_name') or die(mysqli_error($cn));
$sql = "SELECT name FROM category";
$rs = mysqli_query($cn,$sql) or die(mysqli_error($cn));
echo "<select>";
while($row = mysqli_fetch_array($rs)){
echo "<option value='".$row["name"]."'>".$row["name"]."</option>";
}mysqli_free_result($rs);
echo "</select>";

您可以在PHP网站Mysqli Book

上了解mysqli

但是为了安全性和其他好处,您现在应该使用PDO代替