If you have ever used OOP in your programming, I am sure you have experienced the flexibility you get. Also with the introduction of MySQLi class, we did not need to create our own database class anymore since MySQLi comes with almost all the functions you might need. Now the scope of this article is to show you use MySQLi in a class you created that handles something else but still needs a database connection and it is just redundant to create a new connection for every class you have in your code.



Note: Explaining MySqli functions and creating classes are not in the scope of this article and you need to have a good level of PHP knowledge.


Databse Connection

Lets say you have a file named db.php in your file directory. This is all you need in there unless you created a different class that extends the MySQLi :)

<?php
	$mysqli = new mysqli('localhost','username','password','database');
?>




It basically sets “$mysqli” variable to the connection of your database. This is the beauty of mysqli, all you need to do is just set your connection and you have your database class.


Class

Next up is your class. Lets say you have a class named books which gets the records for a book from a table called books from your database.
I saved this file as book.php in my file directory


<?php
class books{
	public $error;
	function __construct($mysqli){}

	function get_book($id){
		global $mysqli;
		if($result = $mysqli->query("SELECT* from books where book_id='$id'")){
			$row=$result->fetch_object();
			return $row;
		}else{
			$this->error = $mysqli->error;
		}

	}
}
?>




Now to explain the code;

Constructor

Following line requires you to send the mysqli connection you created earlier in your db.php. If you want you can have more in your constructor class obviously but in this example I didn’t need to put anything in there. More on Constructors and Destructors

	function __construct($mysqli){}



get_book function:

You need to set your $mysqli variable to be global so you can use it in the function. You accomplish that by just “global $mysqli;” line. If you don’t need a database connection in the function you do not need to set this.



The rest of the function is just basic querying and getting results.


Using the class

In your index.php include both of these files and then assign your class to a variable to be accessed. Notice that we are sending in the $mysqli variable in to the class.


include "db.php";
include "book.php";
$my_book_class = new books($mysqli);




Other Posts

  1. Cool, there is actually some good points on here some of my readers will maybe find this relevant, will send a link, many thanks.

  2. I’m new to php and mysqli. I understand what the script is doing but i do not understand how to sh the data in the index.php page.

      1. you could create a get_books() function kinda similar to the get_book() function i created and then return the resultset and then you can loop through it with a while loop like;

        while ($row = $result->fetch_row()) {
        printf(“%s\n”, $row[0]);
        }

  3. I am still having problems this is what i have so far

    database.php

    members.class.php

    query(“SELECT * FROM users WHERE userid=’$id’”)) {
    return $row;
    } else {
    echo $mysqli->error;
    }
    $mysqli->close();

    }
    }
    ?>

    users.php

    get_user(‘b513273941c5302ecfdbcce45a143b94′);
    echo ‘The users table has: ‘.$result->num_rows.’ users.’;
    while ($row = $result->fetch_object()) {
    echo “”.
    “”.
    “”.
    “User Name:”.
    “”.
    “”.
    “First Name:”.
    “”.
    “”.
    “Last Name:”.
    “”.
    “”.
    “Email:”.
    “”.
    “”.
    “Password:”.
    “”.
    “”.
    “”.
    “”.
    $row->username.
    “”.
    “”.
    $row->name.
    “”.
    “”.
    $row->lastname.
    “”.
    “”.
    $row->email.
    “”.
    “”.
    $row->password.
    “”.
    “”.
    “”;
    }
    ?>

    I get the following errors:

    Fatal error: Call to a member function get_user() on a non-object in C:\Sites\AllFlorida\testsite.com\site\web\users.php on line 5.

    I have been trying to get this to work for 2 days and i cant find anything on the internet
    thanks

  4. the last reply dropped some stuff so here it is again
    I am still having problems this is what i have so far

    database.php

  5. database.php

    include_once(“config.php”);
    $mysqli = new mysqli(SERVER, USER, PASS, DBNAME);
    if (mysqli_connect_errno()) {
    printf(“Connect failed: %s\n”, mysqli_connect_error());
    exit();
    }

  6. members.class.php

    class users{
    public $error;
    function __construct($mysqli){}

    function get_user($id){
    global $mysqli;
    if ($result = $mysqli->query(“SELECT * FROM users WHERE userid=’$id’”)) {
    return $row;
    } else {
    echo $mysqli->error;
    }
    $mysqli->close();

    }
    }

  7. users.php

    include_once(“afcr-includes/database.php”);
    include_once(“afcr-includes/members.class.php”);
    $users = new users($mysqli);
    $result->get_user(‘b513273941c5302ecfdbcce45a143b94′);
    echo ‘The users table has: ‘.$result->num_rows.’ users.’;
    while ($row = $result->fetch_object()) {
    echo “”.
    “”.
    “”.
    “User Name:”.
    “”.
    “”.
    “First Name:”.
    “”.
    “”.
    “Last Name:”.
    “”.
    “”.
    “Email:”.
    “”.
    “”.
    “Password:”.
    “”.
    “”.
    “”.
    “”.
    $row->username.
    “”.
    “”.
    $row->name.
    “”.
    “”.
    $row->lastname.
    “”.
    “”.
    $row->email.
    “”.
    “”.
    $row->password.
    “”.
    “”.
    “”;
    }

    error received:
    Fatal error: Call to a member function get_user() on a non-object in C:\Sites\AllFlorida\testsite.com\site\web\users.php on line 5

  8. try $result = $users->get_user(‘b513273941c5302ecfdbcce45a143b94′)

    instead of $result->get_user(‘b513273941c5302ecfdbcce45a143b94′);

  9. okay i got to work but not sure if it is the right way here it is

    members.class.php

    class members{
    public $error;
    function __construct($mysqli){}

    function get_member($id){
    global $mysqli;
    if($result = $mysqli->query(“SELECT* from users WHERE userid=’$id’”)){
    return $result;
    }else{
    $this->error = $mysqli->error;
    }

    }
    }

    users.php

    include_once(“afcr-includes/database.php”);
    include_once(“afcr-includes/members.class.php”);
    $members = new members($mysqli);
    $result = $members->get_member(‘b513273941c5302ecfdbcce45a143b94′);
    echo ‘The data set has: ‘.$result->num_rows.’ user(s).’;
    while ($row = $result->fetch_object()) {
    echo “User Name: ” .$row->username.”" .
    “First Name: “.$row->name.”" .
    “Last Name: “.$row->lastname.”";
    }

    thanks for the help

  10. This is what I was looking for, thanks!

Leave a Reply