如何将一些旧的MySQL代码转换为MySQLi?

时间:2016-09-20 19:38:36

标签: php mysql mysqli

我注意到一年前我做过的旧请愿网站自PHP服务器升级以来一直没有工作,所以我正在修复它。我已经解决了大部分问题,但现在仍然坚持确认电子邮件。

  

警告:mysqli_num_rows()期望参数1为mysqli_result,   第64行/home/merlin/public_html/index.php中给出的数组

     

警告:mysqli_query()需要至少2个参数,1中给出1   第75行/home/merlin/public_html/index.php

     

警告:mysqli_error()正好需要1个参数,给出0   第75行/home/merlin/public_html/index.php

代码:(我试图解决)

<?php

        if(isset($_GET['key']) ? $_GET['key'] : '') {

            include('database.php');

            $confirmId = (isset($_GET['key']) ? $_GET['key'] : null);
            $errorMessage = "";
            $validCount = 0;

    $sql = "SELECT confirmed FROM email_list WHERE confirmationcode='$confirmId'";
    $sqll = mysqli_query($mysqli,$sql);
    $result = mysqli_fetch_array($sqll);
    $validCount = mysqli_num_rows($result);


            if($result['confirmed'] == 1) {

                echo "<div class=\"notice error\">You have already confirmed this petition signup.</div>";

            } else if($result['confirmed'] == 0){

                $query = "UPDATE email_list SET confirmed = 1 WHERE confirmationcode='$confirmId'";

                mysqli_query($query) OR die(mysqli_error()); 

                echo '<div class=\"notice error\">Your petition has been accepted, Thank you and with your help we might just get Merlin back on our screens. Why not spread the word on Twitter and Facebook? Just quick the icons to the right.</div>';

            }

        }

        ?> 

以前的代码:(转换自)

<?php

        if(isset($_GET['key']) ? $_GET['key'] : '') {

            include('database.php');

            $confirmId = (isset($_GET['key']) ? $_GET['key'] : null);

            $errorMessage = "";
            $validCount = 0;

            $query = "SELECT confirmed FROM email_list WHERE confirmationcode='$confirmId'";
            $result = mysql_query($query);
            $validCount = mysql_num_rows($result);

            if($result['confirmed'] == 1) {

                echo "<div class=\"notice error\">You have already confirmed this petition signup.</div>";

            } else if($result['confirmed'] == 0){

                $query = "UPDATE email_list SET confirmed = 1 WHERE confirmationcode='$confirmId'";

                mysql_query($query) OR die(mysql_error()); 

                echo '<div class=\"notice error\">Your petition has been accepted, Thank you and with your help we might just get Merlin back on our screens. Why not spread the word on Twitter and Facebook? Just quick the icons to the right.</div>';

            }

        }

        ?> 

NEW database.php:

<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

$mysqli = new mysqli("localhost", "merlin_camp", "PASSWORD", "merlin_camp");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

?>

2 个答案:

答案 0 :(得分:0)

你应该使用$ sql(而不是$ result ..因为在你的情况下不是mysqli_query的结果,而是数组形式mysqli_fetch_array)。

$sql = "SELECT confirmed FROM email_list WHERE confirmationcode='$confirmId'";
$sqll = mysqli_query($mysqli,$sql);
$result = mysqli_fetch_array($sqll);
$validCount = mysqli_num_rows($sqll );

答案 1 :(得分:0)

我找到的一个错误会帮助你...这一行:

 mysqli_query($query) OR die(mysqli_error()); 

需要引用连接,应该是:

 mysqli_query($mysqli, $query) OR die(mysqli_error());

mysql和mysqli函数之间的区别是要求在mysqli中引用连接。

当然,为了防止SQL注入,请使用准备好的语句!