Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.8k views
in Technique[技术] by (71.8m points)

php - How to convert PDO to 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 (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();
}

?>

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

Update:

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"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your problem is here:

    $query = mysqli_query("SELECT * FROM users WHERE Username = ? AND Password = ?");

    $userParam = array($_POST["Uname"], $_POST["Pass"]);
    $st = $database->prepare($query);
    $st->execute($userParam);

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.execute.php


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...