Change PDO login script to MySQLi

16
February 12, 2019, at 2:10 PM

I have a problem because I don't know how I should change the ready-made login script with PDO to MySQLi ? Rest of my website is coded in MySQLi. Below its my config file.

<?php
    $DB_host = 'localhost';
    $DB_user = 'root';
    $DB_pass = '';
    $DB_name = 'test_base';
    $mysqli = mysqli_connect($DB_host, $DB_user, $DB_pass, $DB_name); 
?>

session.php

<?php
    require 'cfg.php';
    session_start();
    $username = "";
    $password = "";
    if(isset($_POST['username'])){
        $username = $_POST['username'];
    }
    if (isset($_POST['password'])) {
        $password = $_POST['password'];
    }
    echo $username ." : ".$password;
    $q = 'SELECT * FROM login WHERE username=:username AND password=:password';
    $query = $dbh->prepare($q);
    $query->execute(array(':username' => $username, ':password' => $password));
    if($query->rowCount() == 0){
        header('Location: index.php');
    }
    else{
        $row = $query->fetch(PDO::FETCH_ASSOC);
        session_regenerate_id();
        $_SESSION['sess_user_id'] = $row['id'];
        $_SESSION['sess_username'] = $row['username'];
        $_SESSION['sess_userrole'] = $row['role'];
        echo $_SESSION['sess_userrole'];
        session_write_close();
        if( $_SESSION['sess_userrole'] == "admin"){
            header('Location: admin/index.php');
        }
        else{
            header('Location: user/index.php');
        }
    }
?>

header of each subpages:

<?php 
require 'cfg.php';
session_start();
$role = $_SESSION['sess_userrole'];
if(!isset($_SESSION['sess_username']) && $role!="user"){
    header('Location: index.php');
}
?>
READ ALSO
Yii2&#39;s mongodb collection - get all content of nested arrays

Yii2's mongodb collection - get all content of nested arrays

I have a Yii2 mongodb's collection that looks like this:

44
Mysql subquery or something better

Mysql subquery or something better

I am somewhat new to mysql and I am having an issue on how I should best write the following querySay I have a table that has a datetime column as well as a few others I want to search on

36
How to insert data into two tabels?

How to insert data into two tabels?

I have two tables FILM(Id, Title, Director, Year, Category) and Record(Id, Film_id)

36