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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…