将mysql转换为mysqli的问题

时间:2018-01-01 08:59:54

标签: php mysql

大家好,我目前正处于想要以正确的方式将所有mysql_ *代码转换为mysqli_ *的情况,但我不知道该怎么做。所以我真的需要你的帮助和建议来帮助我的代码。

$sql = "SELECT * FROM studentprofile ";
$query = mysql_query($sql);

if (isset($_POST['search'])) {
    $search_term = mysql_real_escape_string($_POST['search_box']);
    $sql .= "WHERE name = '{$search_term}'";
    $sql .= " OR class = '{$search_term}'";
}

数据输入:

Filter class/name : <input type="text" name="search_box" value="" />

要获取数据,请使用以下代码:

while ($row = mysql_fetch_array($query)) {
<td width="500"><?php echo $row['name']; ?></td>

代码在localhost上工作正常,但每当我将它用于网站主机时,它都需要在mysqli_ *中,我总是遇到这个问题:

  

“mysqli_real_escape_string()需要2个参数,1个给出...”。

感谢。

1 个答案:

答案 0 :(得分:-1)

使用此代码:

$con=mysqli_connect("localhost","username","password","dbname");

$sql = "SELECT * FROM studentprofile ";
$query = mysqli_query($con, $sql);

if (isset($_POST['search'])) {
    $search_term = mysqli_real_escape_string($con, $_POST['search_box']);
    $sql .= "WHERE name = '{$search_term}'";
    $sql .= " OR class = '{$search_term}'";
}

获取数据使用此代码:

<?php  while ($row = mysqli_fetch_array($query,MYSQLI_ASSOC)) {?>
  <td width="500"><?php echo $row['name']; ?></td>
<?php }?>