【问题标题】:Converting MYSQLI TO PDO in PHP在 PHP 中将 MYSQLI 转换为 PDO
【发布时间】:2017-08-21 10:37:26
【问题描述】:

您好我正在尝试创建一个登录系统,我在后端的技能非常有限。我使用教程创建了一个数据库,意识到我在其他页面上的所有 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();
 }


 ?>

【问题讨论】:

  • “如果有人可以编辑代码以使用 PDO”这不是 Stack Overflow 的工作方式。我们不是来为您编写代码的。请使用tour 并阅读help center 中的主题。

标签: php mysql database mysqli pdo


【解决方案1】:

给你..通过比较你的代码来注意变化..要使用 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();
 }


 ?>

【讨论】:

  • 非常感谢你是一个伟大的人!
  • 请不要回答离题的问题。 Stack Overflow 不是我们为彼此编写代码的地方。这是一个学习的地方。好的问题和答案应该适用于将来的其他用户。在这里转储一个完整的解决方案并说“给你,如果你想理解的话,用你的原始代码比较它”不是很有帮助。
  • @Chris 如果你不从某个地方开始,你将无法到达任何地方。
  • @DicedMango,我的意思不是说你不应该问问题。我的观点是您应该阅读How to Ask 并遵循其中包含的指南。如果您询问您收到的具体错误,而不是要求人们为您完成工作,您将学到更多,并且您对 SO 的贡献对未来的访问者将更有价值。这应该是足够的动力,但如果不是,您还应该知道,像这样的问题经常会获得反对票并被关闭,甚至被删除。
猜你喜欢
  • 2012-02-26
  • 1970-01-01
  • 2020-03-26
  • 1970-01-01
  • 2019-11-07
  • 2015-04-05
  • 2015-01-25
  • 2019-12-19
  • 2017-09-27
相关资源
最近更新 更多