-1

我正在尝试将下面的 mysql 更改为 mysqli(准备好的语句),但我陷入了困境。

问题: Warning: mysqli_stmt ::bind_result(): Number of bind variables doesn't match number of fields in prepared statement这个错误发生在 $get_empty_field = $ok->bind_result($username, $firstname, $lastname); 线。我还没有完成转换,我只是做了我能做的,其余的保持原样。(我没有混合 mysql 和 mysqli)

mysql查询

$check_added_files = mysql_query("select * from `vpb_uploads` where `username` = '".mysql_real_escape_string($username)."' and `firstname` = '' and `image_one` != '' and `image_two` != '' and `image_three` != '' and `image_four` != '' and `image_five` != ''");
                    if(mysql_num_rows($check_added_files) == 1)
                    {
                    echo 'up_to_five_already';
                    }
                    else
                    {
                    if (move_uploaded_file($_FILES['file_to_upload']['tmp_name'], $final_uploads_location)) 
                    {
                    $check_empty_field = mysql_query("select * from `vpb_uploads` where `username` = '".mysql_real_escape_string(strip_tags($username))."'  and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");
                    if(mysql_num_rows($check_empty_field) < 1)
                    {
                    mysql_query("insert into `vpb_uploads` values('', '".mysql_real_escape_string($username)."', '', '', '".mysql_real_escape_string($random_name_generated)."', '', '', '', '', '".mysql_real_escape_string(date("d-m-Y"))."')");

                    $identity = "image_one";
                    }
                    else
                    {
                    $get_empty_field = mysql_fetch_array($check_empty_field);
                    $image_one = strip_tags($get_empty_field["image_one"]);
                    $image_two = strip_tags($get_empty_field["image_two"]);
                    $image_three = strip_tags($get_empty_field["image_three"]);
                    $image_four = strip_tags($get_empty_field["image_four"]);
                    $image_five = strip_tags($get_empty_field["image_five"]);
                    global $identity;

                    if(empty($image_one))
                    {
                        mysql_query("update `vpb_uploads` set `image_one` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");

                        $identity = "image_one";

                    }
                    elseif(empty($image_two))
                    {
                        mysql_query("update `vpb_uploads` set `image_two` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");

                        $identity = "image_two";
                    }
                    elseif(empty($image_three))
                    {
                        mysql_query("update `vpb_uploads` set `image_three` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");

                        $identity = "image_three";
                    }
                    elseif(empty($image_four))
                    {
                        mysql_query("update `vpb_uploads` set `image_four` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");

                        $identity = "image_four";
                    }
                    elseif(empty($image_five))
                    {
                        mysql_query("update `vpb_uploads` set `image_five` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");

                        $identity = "image_five";
                    }

到目前为止我有什么:

$mysqli = new mysqli("localhost", "root", "", "newlogin");
                if ($mysqli->connect_errno) {
                echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
                }


                $stmt = $mysqli->prepare("select * from `vpb_uploads` where `username` = ? and `firstname` = '' and `image_one` != '' and `image_two` != '' and `image_three` != '' and `image_four` != '' and `image_five` != ''");
                $stmt->bind_param('s', $username);
                $stmt->execute();
                $stmt->store_result();

                if ($stmt->num_rows == 1) {

                echo 'up_to_five_already';
                }


                else
                {
                if (move_uploaded_file($_FILES['file_to_upload']['tmp_name'], $final_uploads_location)) 
                {
                $firstname = "''";
                $lastname = "''";
                $stmt = $mysqli->prepare("select * from `vpb_uploads` where `username` = ?  and `firstname` = ? and `lastname` = ?");
                $stmt->bind_param('sss', $username, $firstname, $lastname);
                $stmt->execute();
                $stmt->store_result();
                if ($stmt->num_rows < 1)

                {
                $date = 'date("d-m-Y")';
                $image_2 = "''";
                $image_3 = "''";
                $image_4 = "''";
                $image_5 = "''";
                $stmt = $mysqli->prepare("insert into `vpb_uploads` (`username`, `firstname`, `lastname`, `image_one`, `image_two`, `image_three`, `image_four`, `image_five`, `date`) values(?,?,?,?,?,?,?,?,?)");
                $stmt->bind_param('sssssssss',  $username, $firstname, $lastname, $random_name_generated, $image_2, $image_3, $image_4, $image_5, $date);
                $stmt->execute();


                $identity = "image_one";
                }
                else
                {

                $get_empty_field = $stmt->bind_result($username, $firstname, $lastname);
                $image_one = strip_tags($get_empty_field["image_one"]);
                $image_two = strip_tags($get_empty_field["image_two"]);
                $image_three = strip_tags($get_empty_field["image_three"]);
                $image_four = strip_tags($get_empty_field["image_four"]);
                $image_five = strip_tags($get_empty_field["image_five"]);
                global $identity;

               if(empty($image_one))
                    {
                $stmt = $mysqli->prepare("update `vpb_uploads` set `image_one` = ? where `username` = ? and `firstname` = ? and `lastname` = ? ");
                $stmt->bind_param('ssss', $random_name_generated, $username, $firstname, $lastname);    
                $stmt->execute();
                $stmt->close();

                $identity = "image_one";

                }
                elseif(empty($image_two))
                {
                mysql_query("update `vpb_uploads` set `image_two` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");

                $identity = "image_two";
                }
                elseif(empty($image_three))
                {
                mysql_query("update `vpb_uploads` set `image_three` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");

                $identity = "image_three";
                }
                elseif(empty($image_four))
                {
                mysql_query("update `vpb_uploads` set `image_four` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");

                $identity = "image_four";
                }
                elseif(empty($image_five))
                {
                mysql_query("update `vpb_uploads` set `image_five` = '".mysql_real_escape_string($random_name_generated)."' where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");

                $identity = "image_five";
                }
4

2 回答 2

1

正如我之前告诉你的,你必须更改为 PDO,而不是 mysqli。当然,仅仅复制代码是没有用的。您至少需要先连接到 PDO。我提供了一个指向 PDO 标签 wiki 的链接,其中包含要复制的完整连接代码。此外,除了复制代码之外,您还需要花费一些自己的努力来使其工作 - 至少通过调整您的数据库设置。

通常,您在此处获得的代码并不是开箱即用的。他们是给你一个想法,让你学习。这是非常重要的事情——你需要从答案中学习,才能自己做下一个类似的工作。如果您只是复制并粘贴代码,您将要求 Stack Overflow 重写您的所有应用程序。请从答案中学习并尝试利用您获得的知识。

于 2013-03-29T05:22:25.603 回答
0

您将需要更改您的SELECT声明:

$stmt = $mysqli->prepare("select `username`,`firstname`,`lastname` from `vpb_uploads` where `username` = ?  and `firstname` = ? and `lastname` = ?");

您将需要修复变量名称:

$get_empty_field = $stmt->bind_result($username, $firstname, $lastname);
于 2013-03-29T05:12:36.657 回答