在 PHP 中将 MYSQLI 转换为 PDO

问题描述 投票:0回答:2

您好,我正在尝试创建一个登录系统,我在后端的技能非常有限。我使用了一个教程来创建一个数据库,意识到我其他页面上的所有 php 都使用了 pdo,其中本教程是 mysqli。

我尝试修改这段代码来尝试调整它,但是我的尝试并没有成功。

真的厚颜无耻,但如果有人可以编辑代码以使用 PDO,我将不胜感激:)。

非常感谢

<?php
 try {
   $handler = new PDO('mysql:host=127.0.0.1;dbname=loginsytem', 'root', '');
   $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 } catch(PDOException $e){
   echo $e->getName();
   die();
 }

 session_start();

 $query = $handler->query('SELECT * FROM users');

 if (isset($_POST['submit'])) {

   incude_'dbh.inc.php';

   $uid = PDO::quote($conn, $_POST['uid']);
   $pwd = PDO::quote($conn, $_POST['pwd']);

   //Error Handlers
   //Check if inputs are empty

   if (empty($uid)) || empty($pwd)) {
     header("location: ../index.php?login=empty");
     exit();
   }
 }   else {
     $sql = "SELECT * FROM users WHERE user_uid='$uid'";
     $result = mysqli_query($conn, $sql);
     $resultCheck = mysqli_num_rows($result);
     if ($resultCheck < 1) {
       header("location: ../index.php?login=error");
       exit();
     } else {
       if ($row = mysqli_fetch_assoc($result)) {
         //de-hashing the password
         $hashedPwdCheck = password_verify($pwd, $row['user_pdw']);
         if ($hashedPwdCheck == false) {
           header("location: ../index.php?login=error");
           exit();

         } elseif ($hashedPwdCheck == true) {
           //Log in the user here
           $_SESSION['u_id'] = $row['user_id'];
           $_SESSION['u_uid'] = $row['user_uid'];
           header("location: ../index.php?login=success");
           exit();
         }
       }
     }
   }

   else {
   header("location: ../index.php?login=error");
   exit();
 }


 ?>
php mysql database mysqli pdo
2个回答
4
投票

开始吧.. 通过比较代码来注意更改。. 要使用 MySQL 学习 PDO,请参阅本教程 http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

<?php
 try {
   $handler = new PDO('mysql:host=127.0.0.1;dbname=loginsytem', 'root', '');
   $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 } catch(PDOException $e){
   echo $e->getName();
   die();
 }

 session_start();

 //$query = $handler->query('SELECT * FROM users');

 if (isset($_POST['submit'])) {

   //Error Handlers
   //Check if inputs are empty

   if (empty($uid)) || empty($pwd)) {
     header("location: ../index.php?login=empty");
     exit();
   }
 }   else {

     $stmt = $db->prepare("SELECT * FROM users WHERE user_uid=:uid");
     $stmt->bindParam(':uid', $uid, PDO::PARAM_STR);

     if ($stmt->execute()) {
       header("location: ../index.php?login=error");
       exit();
     } else {
       if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
         //de-hashing the password
         $hashedPwdCheck = password_verify($pwd, $row['user_pdw']);
         if ($hashedPwdCheck == false) {
           header("location: ../index.php?login=error");
           exit();

         } elseif ($hashedPwdCheck == true) {
           //Log in the user here
           $_SESSION['u_id'] = $row['user_id'];
           $_SESSION['u_uid'] = $row['user_uid'];
           header("location: ../index.php?login=success");
           exit();
         }
       }
     }
   }

   else {
   header("location: ../index.php?login=error");
   exit();
 }


 ?>

2
投票

或者您可以使用以下MySQLi 到 PDO 适配器,无需任何更改:

require_once 'MySQLi_PDO_Adapter.php';

$handler = new PDO('mysql:host=127.0.0.1;dbname=loginsytem', 'root', '');
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$mysqli = new MySQLi\PDO\Adapter\Connection($handler);

$query = $mysqli->query('SELECT * FROM users');
while ($row = $query->fetch_assoc()) {
    ...
}
© www.soinside.com 2019 - 2024. All rights reserved.