3

I have this external file CreateConnection.php that has a class DBController and its following functions. The createconnection file encounters no errors.

CreateConnection.php file inside

<?php
class DBController
{
    private $servername = "localhost";
    private $username = "root";
    private $password = "";
    private $database = "mydatabase";

    function mainConnect()
    {
        //call the function connectDatabase to $connect
        $connect = $this->connectDatabase();

       //call the function selectDatabase
        $this->selectDatabase($connect);

        if(!$connect)
        {
            //Otherwise, prompt connection failed
            die("Connection failed: ".mysqli_connect_error());
        }
    }

    function connectDatabase()
    {
        //Create connection
        $connect = mysqli_connect($this->servername, $this->username, $this->password);
        return $connect;
    }

    function selectDatabase($connect)
    {
         //Select database
         mysqli_select_db($connect, $this->database);
    }
}
?>

In this file, I include the external CreateConnection.php , but my '$connect' encounters many errors when calling the mainConnect() function. Process.php file

<?php
    include("CreateConnection.php");
    $DBHandler = new DBController();

    $connect = $DBHandler->mainConnect();

    //Get values from form LoginReminder.php file
    $username = mysqli_real_escape_string($connect, $_POST['username']);
    $password = mysqli_real_escape_string($connect, $_POST['password']);
    //Removes back slashes in input
    $username = stripcslashes($username);
    $password = stripcslashes($password);

    //Query the database for users
    $result = mysqli_query($connect,"select * from tablereminders where username = '$username' and password = '$password' ");

    //Error connection and query
    if (!$result)
    {
        printf("Error: %s\n", mysqli_error($connect));
        exit();
    }
?>

Does my syntax is incorrect since I've encountered this many errors when submitting a form:

Errors

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\Web Php Tutorial\LoginProcess.php on line 8

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\Web Php Tutorial\LoginProcess.php on line 9

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\Web Php Tutorial\LoginProcess.php on line 15

Warning: mysqli_error() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\Web Php Tutorial\LoginProcess.php on line 20
laurence keith albano
  • 1,409
  • 4
  • 27
  • 59

1 Answers1

7

Add return $connect; at the end of function mainConnect(){ and you are fine.

But why do you not use OOP Version of MYSQLi at all?

mysqli, OOP vs Procedural

class DBController
{ 
   private $db;

   private $servername = "localhost";
   private $username = "root";
   private $password = "";
   private $database = "mydatabase";

   function __construct(){
      $this->db = new mysqli($this->servername, $this->username, $this->password);
      $this->db->select_db($this->database);
   }
   function db(){
      return $this->db;
   }

}
$connection = new DBController();

#now instead of  using mysqli_query();
$connection->db()->query('SQL QUERY');

And a file thats holding only a class should have this as name:

Instead of CreateConnection.php better DBController.php

The filename sounds like it will create a connection when its included, but that not the case :)

Community
  • 1
  • 1
JustOnUnderMillions
  • 3,741
  • 9
  • 12