<small id='Nywg1'></small><noframes id='Nywg1'>

    1. <i id='Nywg1'><tr id='Nywg1'><dt id='Nywg1'><q id='Nywg1'><span id='Nywg1'><b id='Nywg1'><form id='Nywg1'><ins id='Nywg1'></ins><ul id='Nywg1'></ul><sub id='Nywg1'></sub></form><legend id='Nywg1'></legend><bdo id='Nywg1'><pre id='Nywg1'><center id='Nywg1'></center></pre></bdo></b><th id='Nywg1'></th></span></q></dt></tr></i><div id='Nywg1'><tfoot id='Nywg1'></tfoot><dl id='Nywg1'><fieldset id='Nywg1'></fieldset></dl></div>
      <legend id='Nywg1'><style id='Nywg1'><dir id='Nywg1'><q id='Nywg1'></q></dir></style></legend>
          <bdo id='Nywg1'></bdo><ul id='Nywg1'></ul>

      1. <tfoot id='Nywg1'></tfoot>

        如何将 PDO 转换为 mysqli?

        时间:2023-09-25

        <i id='gNfcH'><tr id='gNfcH'><dt id='gNfcH'><q id='gNfcH'><span id='gNfcH'><b id='gNfcH'><form id='gNfcH'><ins id='gNfcH'></ins><ul id='gNfcH'></ul><sub id='gNfcH'></sub></form><legend id='gNfcH'></legend><bdo id='gNfcH'><pre id='gNfcH'><center id='gNfcH'></center></pre></bdo></b><th id='gNfcH'></th></span></q></dt></tr></i><div id='gNfcH'><tfoot id='gNfcH'></tfoot><dl id='gNfcH'><fieldset id='gNfcH'></fieldset></dl></div>
          <tbody id='gNfcH'></tbody>
      2. <legend id='gNfcH'><style id='gNfcH'><dir id='gNfcH'><q id='gNfcH'></q></dir></style></legend>

          <tfoot id='gNfcH'></tfoot>

            • <small id='gNfcH'></small><noframes id='gNfcH'>

                • <bdo id='gNfcH'></bdo><ul id='gNfcH'></ul>
                  本文介绍了如何将 PDO 转换为 mysqli?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个登录屏幕,用户可以在其中输入用户名和密码.我设法使用 PDO 连接到数据库,但我必须将其更改为 mysqli.有人能帮我把它转换成mysqli吗?提前致谢.

                  I have a login screen in which the user inputs their username and password. I managed to connect to the database using PDO but I have to change it to mysqli. Could someone please help me convert it to mysqli. Thanks in advance.

                  PDO:

                  <?php
                  try {
                          $database = new PDO('mysql:host=localhost;dbname=myfiles', 'root', '',array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
                          $query = "SELECT * FROM users WHERE Username = ? AND Password = ?";
                  
                          $userParam = array($_POST["Uname"], $_POST["Pass"]);
                          $st = $database->prepare($query);
                          $st->execute($userParam);
                  
                          $getResults = $st->fetch(PDO::FETCH_ASSOC);
                  
                          session_start();
                  
                          $_SESSION['details']['username'] = $getResults['Username'];
                          $_SESSION['details']['password'] = $getResults['Password'];
                  
                          unset($database);
                          header("Location: index.php");
                      }
                  catch(PDOException $e)
                      {
                     print "Error!: " . $e->getMessage() . "<br/>";
                      exit();
                      }
                    ?>
                  

                  Mysqli(这是我尝试过的)

                  Mysqli (This is what I have tried)

                  <?php
                  
                  try {
                  
                  $database =  mysqli_connect("localhost", "root", "", "myfiles");
                  $query = mysqli_query("SELECT * FROM users WHERE Username = ? AND Password = ?");
                  
                          $userParam = array($_POST["Uname"], $_POST["Pass"]);
                          $st = $database->prepare($query);
                          $st->execute($userParam);
                  
                          $getResults = mysqli_fetch_assoc($query);
                  
                          session_start();
                  
                          $_SESSION['details']['username'] = $results['Username'];
                          $_SESSION['details']['password'] = $results['Password'];
                  
                          unset($database);
                          header("Location: index.php");
                  
                          } catch (Exception $e ) {
                          print "Error!: " . $e->getMessage() . "<br/>";
                          exit();
                  }
                  
                  ?>
                  

                  运行Mysqli代码时出错:

                  Error when running Mysqli code:

                  Warning: mysqli_query() expects at least 2 parameters, 1 given in G:xampphtdocsolddroplogin.php on line 39
                  
                  Fatal error: Call to a member function execute() on a non-object in G:xampphtdocsolddroplogin.php on line 43
                  

                  更新:

                  Mysqli

                  <?php
                  
                         try {
                  
                      $database =  mysqli_connect("localhost", "root", "", "myfiles");
                      $query = "SELECT * FROM users WHERE Username = ? AND Password = ?";
                  
                              $userParam = array($_POST["Uname"], $_POST["Pass"]);
                              $st = $database->prepare($query);
                              $st->execute($userParam);
                  
                              $getResults = mysqli_fetch_assoc($st);
                  
                              session_start();
                  
                              $_SESSION['details']['username'] = $getResults['Username'];
                              $_SESSION['details']['password'] = $getResults['Password'];
                  
                              unset($database);
                              header("Location: index.php");
                  
                              } catch (Exception $e ) {
                              print "Error!: " . $e->getMessage() . "<br/>";
                              exit();
                      }
                  
                      ?>
                  
                      "Waiting for localhost"
                  

                  推荐答案

                  你的问题在这里:

                      $query = mysqli_query("SELECT * FROM users WHERE Username = ? AND Password = ?");
                  
                      $userParam = array($_POST["Uname"], $_POST["Pass"]);
                      $st = $database->prepare($query);
                      $st->execute($userParam);
                  

                  prepare 需要一个字符串,mysqli_query 是一个执行查询的过程函数.去掉那个.此外,使用 mysqli,您需要先绑定参数,而不是将它们传递给 execute().

                  prepare expects a string, mysqli_query is a procedural function which executes the query. Remove that. Additionally, with mysqli you need to bind the parameters first, not pass them to execute().

                  $query = "SELECT * FROM users WHERE Username = ? AND Password = ?";
                  
                  $st = $database->prepare($query);
                  $st->bindParam("ss",$_POST["Uname"], $_POST["Pass"]);    
                  $st->execute();
                  

                  以下是手册的相关部分:

                  Here's the relevant sections of the manual:

                  http://php.net/manual/en/mysqli.prepare.php

                  http://php.net/manual/en/mysqli-stmt.bind-param.php

                  http://php.net/manual/en/mysqli-stmt.执行.php

                  这篇关于如何将 PDO 转换为 mysqli?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在布尔值上调用成员函数 bind_param() 下一篇:使用 Blob 将图像上传到 MySQL 数据库

                  相关文章

                  最新文章

                    <bdo id='t9653'></bdo><ul id='t9653'></ul>

                  <small id='t9653'></small><noframes id='t9653'>

                  <tfoot id='t9653'></tfoot>

                  1. <i id='t9653'><tr id='t9653'><dt id='t9653'><q id='t9653'><span id='t9653'><b id='t9653'><form id='t9653'><ins id='t9653'></ins><ul id='t9653'></ul><sub id='t9653'></sub></form><legend id='t9653'></legend><bdo id='t9653'><pre id='t9653'><center id='t9653'></center></pre></bdo></b><th id='t9653'></th></span></q></dt></tr></i><div id='t9653'><tfoot id='t9653'></tfoot><dl id='t9653'><fieldset id='t9653'></fieldset></dl></div>

                      <legend id='t9653'><style id='t9653'><dir id='t9653'><q id='t9653'></q></dir></style></legend>