0

I'm just starting with OOP, and while using a tutorial I encountered the following errors:

Warning: Missing argument 2 for MySQLDatabase::query(), called in
/Applications/MAMP/htdocs/object-oriented/public/index.php on line 7 and defined in
/Applications/MAMP/htdocs/object-oriented/includes/database.php on line 28

Notice: Undefined variable: sql in
/Applications/MAMP/htdocs/object-oriented/includes/database.php on line 29

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in
/Applications/MAMP/htdocs/object-oriented/includes/database.php on line 29

Here is my database.php

<?php

require('config.php');

class MySQLDatabase {

    public $connection;

    public function open_connection() {
        $this->$connection = mysqli_connect('DB_SERVER', 'DB_USER', 'DB_PASS');
        if (!$this->connection) {
            die("Database connection failed");
        } else {
            $db_select = mysqli_select_db($this->connection, DB_NAME);
            if (!$select_db) {
                die("Database selection failed");
            }
        }
    }

    public function close_connection() {
        if (isset($this->connection)) {
            mysqli_close($this->connection);
            unset($this->connection);
        }
    }

    public function query($connection, $sql) {
        $result = mysqli_query($this->connection, $sql);
        $this->confirm_query($result);
        return $result;
    }

and my index.php in which I'm trying to test the query() method.

$sql = "INSERT INTO users (id, username, password, first_name, last_name) ";
$sql .= "VALUES (1, 'andrei', 'password', 'Andrei', 'Popa')";
$result = $database->query($sql);

$sql = "SELECT * FROM users WHERE id = 1";
$result_set = $database->query($sql);
$found_user = mysqli_fetch_array($result_set);
echo "Found user!" . $found_user['username'];

Any idea what I'm doing wrong? Thanks

2
  • Your query function expects two parameters. You're passing it one - though you don't need to pass it $connection, as it's never used (you're using the object's $connection property instead) Commented Dec 16, 2013 at 19:42
  • read the error messages, they seem rather clear Commented Dec 16, 2013 at 19:42

3 Answers 3

1
public function query($connection, $sql) {

You don't need the first parameter, as the the connection is property of your class. So only pass $sql into the query method as you already did when using the method:

public function query($sql) {
Sign up to request clarification or add additional context in comments.

1 Comment

Fixed that. Now I get Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /Applications/MAMP/htdocs/object-oriented/includes/database.php on line 29
1

This is your definition:

public function query($connection, $sql) {

and this is your call:

$result = $database->query($sql);

You need to add a connection :)

Comments

0

You have a number of errors:

First

$db_select = mysqli_select_db($this->connection, DB_NAME);
if (!$select_db) {

check varibables db_select and select_db

Second:

public function query($connection, $sql) {

$result = $database->query($sql);

you need to check your arguments. I think, you need to remove $connection from declaration

public function query($sql) {

because next you are using class variable:

$this->connection;

Third:

public function open_connection()

there is no counstructor in your class and you don't call method open_connection, as a result: you active connection is NULL

You need to add this method to your class:

function __construct() {
    open_connection();
}

2 Comments

Fixed the problems, sorry about $select_db, typo. After using the construct method i end up with: ` Fatal error: Call to undefined function open_connection() in /Applications/MAMP/htdocs/object-oriented/includes/database.php on line 59`
Sorry, $this->open_connection(); of cause.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.