Converting MYSQL to MYSQLI

Could anyone please help me to convert the below file from MYSQL to MYSQLI, I have replaced MYSQL with MYSQLI, however I have read about MYSQLI which requires two parameters as only one is required for MYSQL. Could someone please help me with the below file, as I need to convert it from MYSQL to MYSQLI or PDO. I have attached the file so that somebody is able to help me and convert it.

[php]<?php

/**

  • Database config variables
    */
    define(“DB_HOST”, “127.0.0.1”);
    define(“DB_USER”, “User”);
    define(“DB_PASSWORD"Password”);
    define(“DB_DATABASE”, “bradvisor_login_api”);
    ?>
    [/php]

[php]<?php
class DB_Connect {

// constructor
function __construct() {
    
}

// destructor
function __destruct() {
    // $this->close();
}

// Connecting to database
public function connect() {
    require_once 'include/config.php';
    // connecting to mysql
    $con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
    // selecting database
    mysqli_select_db($con, "DB_DATABASE");
	
 // return database handler
    return $con;
}

// Closing database connection
public function close() {
    mysqli_close();
}

}

?>[/php]

[php]<?php

class DB_Functions {

private $db;

//put your code here
// constructor
function __construct() {
    require_once 'DB_Connect.php';
    // connecting to database
    $this->db = new DB_Connect();
    $this->db->connect();
}

// destructor
function __destruct() {
    
}


/**
 * Random string which is sent by mail to reset password
 */

public function random_string()
{
$character_set_array = array();
$character_set_array[] = array(‘count’ => 7, ‘characters’ => ‘abcdefghijklmnopqrstuvwxyz’);
$character_set_array[] = array(‘count’ => 1, ‘characters’ => ‘0123456789’);
$temp_array = array();
foreach ($character_set_array as $character_set) {
for ($i = 0; $i < $character_set[‘count’]; $i++) {
$temp_array[] = $character_set[‘characters’][rand(0, strlen($character_set[‘characters’]) - 1)];
}
}
shuffle($temp_array);
return implode(’’, $temp_array);
}

public function forgotPassword($forgotpassword, $newpassword, $salt){
$result = mysqli_query(“UPDATE users SET encrypted_password = ‘$newpassword’,salt = ‘$salt’
WHERE email = ‘$forgotpassword’”);

if ($result) {

return true;

}
else
{
return false;
}

}
/**
* Adding new user to mysqli database
* returns user details
*/

public function storeUser($fname, $lname, $email, $uname, $password) {
    $uuid = uniqid('', true);
    $hash = $this->hashSSHA($password);
    $encrypted_password = $hash["encrypted"]; // encrypted password
    $salt = $hash["salt"]; // salt
    $result = $this->db->query("INSERT INTO users(unique_id, firstname, lastname, email, username, encrypted_password, salt, created_at) VALUES('$uuid', '$fname', '$lname', '$email', '$uname', '$encrypted_password', '$salt', NOW())");      

// check for successful store
if ($result) {
// get user details
$uid = mysqli_insert_id(); // last inserted id
$result = mysqli_query(“SELECT * FROM users WHERE uid = $uid”);
// return user details
return mysqli_fetch_array($result);
} else {
return false;
}
}

/**
 * Verifies user by email and password
 */
public function getUserByEmailAndPassword($email, $password) {
    $result = mysqli_query("SELECT * FROM users WHERE email = '$email'") or die(mysqli_error());
    // check for result 
    $no_of_rows = mysqli_num_rows($result);
    if ($no_of_rows > 0) {
        $result = mysqli_fetch_array($result);
        $salt = $result['salt'];
        $encrypted_password = $result['encrypted_password'];
        $hash = $this->checkhashSSHA($salt, $password);
        // check for password equality
        if ($encrypted_password == $hash) {
            // user authentication details are correct
            return $result;
        }
    } else {
        // user not found
        return false;
    }
}

/**
* Check user is existed or not
*/
public function isUserExisted($email) {
$result = mysqli_query(“SELECT email from users WHERE email = ‘$email’”);
$no_of_rows = mysqli_num_rows($result);
if ($no_of_rows > 0) {
// user existed
return true;
} else {
// user not existed
return false;
}
}

/**
 * Encrypting password
 * returns salt and encrypted password
 */
public function hashSSHA($password) {

    $salt = sha1(rand());
    $salt = substr($salt, 0, 10);
    $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
    $hash = array("salt" => $salt, "encrypted" => $encrypted);
    return $hash;
}

/**
 * Decrypting password
 * returns hash string
 */
public function checkhashSSHA($salt, $password) {

    $hash = base64_encode(sha1($password . $salt, true) . $salt);

    return $hash;
}

}

?>
[/php]

You have an error here:

[php] define("DB_PASSWORD"Password");[/php]
I have replaced MYSQL with MYSQLI, however I have read about MYSQLI which requires two parameters as only one is required for MYSQL.

Okay, so add the other parameter in.

My Problem is that I dont know how to add the other parameters in, could you please look at the below and please advise me.

[php]<?php

class DB_Functions {

private $db;

//put your code here
// constructor
function __construct() {
    require_once 'DB_Connect.php';
    // connecting to database
    $this->db = new DB_Connect();
    $this->db->connect();
}

// destructor
function __destruct() {
    
}


/**
 * Random string which is sent by mail to reset password
 */

public function random_string()
{
$character_set_array = array();
$character_set_array[] = array(‘count’ => 7, ‘characters’ => ‘abcdefghijklmnopqrstuvwxyz’);
$character_set_array[] = array(‘count’ => 1, ‘characters’ => ‘0123456789’);
$temp_array = array();
foreach ($character_set_array as $character_set) {
for ($i = 0; $i < $character_set[‘count’]; $i++) {
$temp_array[] = $character_set[‘characters’][rand(0, strlen($character_set[‘characters’]) - 1)];
}
}
shuffle($temp_array);
return implode(’’, $temp_array);
}

public function forgotPassword($forgotpassword, $newpassword, $salt){
$result = $this->db->query(“UPDATE users SET encrypted_password = ‘$newpassword’,salt = ‘$salt’
WHERE email = ‘$forgotpassword’”);

if ($result) {

return true;

}
else
{
return false;
}

}
/**
* Adding new user to mysqli database
* returns user details
*/

public function storeUser($fname, $lname, $email, $uname, $password) {
    $uuid = uniqid('', true);
    $hash = $this->hashSSHA($password);
    $encrypted_password = $hash["encrypted"]; // encrypted password
    $salt = $hash["salt"]; // salt
    $result = $this->db->query("INSERT INTO users(unique_id, firstname, lastname, email, username, encrypted_password, salt, created_at) VALUES('$uuid', '$fname', '$lname', '$email', '$uname', '$encrypted_password', '$salt', NOW())");      

// check for successful store
if ($result) {
// get user details
$uid = mysqli_insert_id($this->db) // last inserted id
$result = $this->db->query(“SELECT * FROM users WHERE uid = $uid”);
// return user details
return mysqli_fetch_array($result);
} else {
return false;
}
}

/**
 * Verifies user by email and password
 */
public function getUserByEmailAndPassword($email, $password) {
      $result = $this->db->query("SELECT * FROM users WHERE email = '$email'") or die(mysqli_error());
    // check for result 
    $no_of_rows = mysqli_num_rows($result);
    if ($no_of_rows > 0) {
        $result = mysqli_fetch_array($result);
        $salt = $result['salt'];
        $encrypted_password = $result['encrypted_password'];
        $hash = $this->checkhashSSHA($salt, $password);
        // check for password equality
        if ($encrypted_password == $hash) {
            // user authentication details are correct
            return $result;
        }
    } else {
        // user not found
        return false;
    }
}

/**
* Check user is existed or not
*/
public function isUserExisted($email) {
$result = $this->db->query(“SELECT email from users WHERE email = ‘$email’”);
$no_of_rows = mysqli_num_rows($result);
if ($no_of_rows > 0) {
// user existed
return true;
} else {
// user not existed
return false;
}
}

/**
 * Encrypting password
 * returns salt and encrypted password
 */
public function hashSSHA($password) {

    $salt = sha1(rand());
    $salt = substr($salt, 0, 10);
    $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
    $hash = array("salt" => $salt, "encrypted" => $encrypted);
    return $hash;
}

/**
 * Decrypting password
 * returns hash string
 */
public function checkhashSSHA($salt, $password) {

    $hash = base64_encode(sha1($password . $salt, true) . $salt);

    return $hash;
}

}

?>
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service