Mysqli connection in function PHP

2.6k views Asked by At

I'm facing a PHP problem. I've searched the web but couldn't find the answer. This is my code so far:

<?php
   $db_host = 'db_host';
   $db_user = 'db_user';
   $db_password = 'db_password';
   $db_name = 'db_name';
   //not showing you the real db login ofcourse

   $conn = mysqli_connect($db_host, $db_user, $db_password, $db_name);
   if($conn) {
      echo 'We are connected!';
   }

Up to here, everyting goes well. The connection is established and 'We are connected!' appears on the screen.

function login($username, $password, $conn) {
   $result = $conn->query("SELECT * FROM users");
   echo mysqli_errno($conn) . mysqli_error($conn);
} 

When I run this function however, the mysqli error 'No database selected pops up. So I added the following piece of code the the file before and in the function, so the total code becomes:

<?php
   $db_host = 'db_host';
   $db_user = 'db_user';
   $db_password = 'db_password';
   $db_name = 'db_name';
   //not showing you the real db login ofcourse

   $conn = mysqli_connect($db_host, $db_user, $db_password, $db_name);
   if($conn) {
      echo 'We are connected!';
   }

   if (!mysqli_select_db($conn, $db_name)) {
      die("1st time failed");
   }

   function login($username, $password, $conn, $db_name) {
      if (!mysqli_select_db($conn, $db_name)) {
         die("2nd time failed");
      }
      $result = $conn->query("SELECT * FROM users");
      echo mysqli_errno($conn) . mysqli_error($conn);
   } 

   $username = 'test';
   $password = 'test';
   login($username, $password, $conn, $db_name);
?>

The first time adding the database name works fine, however, in the function it doesn't work. I've also tried using global $conn inside the function but that didn't work either. Changing mysqli_connect() to new mysqli() also doesn't have any effect.

Thanks in advance!

1

There are 1 answers

8
Leandro Papasidero On BEST ANSWER

Please be aware, this code is refactored based on your code, and the login logic is NOT RECOMMENDED. Please try this code and make the changes that you think you need.

Make sure that your Database information is also updated as needed.

MyDB Class

Class MyDB {

protected $_DB_HOST = 'localhost';
protected $_DB_USER = 'user';
protected $_DB_PASS = 'password';
protected $_DB_NAME = 'table_name';
protected $_conn;

public function __construct() {
    $this->_conn = mysqli_connect($this->_DB_HOST, $this->_DB_USER, $this->_DB_PASS);
    if($this->_conn) {
        echo 'We are connected!<br>';
    }
}

public function connect() {
    if(!mysqli_select_db($this->_conn, $this->_DB_NAME)) {
        die("1st time failed<br>");
    }

    return $this->_conn;
}

}

Login Class

Class Login {

protected $_conn;

public function __construct() {
    $db = new MyDB();
    $this->_conn = $db->connect();
}

//This is a HORRIBLE way to check your login. Please change your logic here. I am just kind of re-using what you got
public function login($username, $password) {
    $result = $this->_conn->query("SELECT * FROM user WHERE username ='$username' AND password='$password'");

    if(!$result) {
        echo mysqli_errno($this->_conn) . mysqli_error($this->_conn);
        return false;
    }

    return $result->fetch_row() > 0;
}

}

Usage

$login = new Login();
$logged = $login->login('username', 'password');

if ($logged) {
    echo "yeah!! you are IN";
} else {
    echo "boo!! . Wrong username and password";
}