如何将 PDO 转换为 mysqli?

时间:2023-03-06
本文介绍了如何将 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

相关文章