-2

I have 2 classes, controller and connector. They are used by delete.php. I got error saying

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in             C:\xampp\htdocs\pracownia\kontroler.php on line 38
Warning: mysqli_error() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\pracownia\kontroler.php on line 44

Does anyone know here did I make mistake? For me it looks like the link to the connection is badly passed to the controller class somewhere. Or the return doesnt work as I wanted. It all did work before I had to make it into classes...

class connector is here:

<?php

class connector {

private $db_handle="";
private $user_name;
private $password;
private $database;
public static $server = "127.0.0.1";

function __construct($user_name="", $password="", $database="") 
{
    $this->user_name = $user_name;
    $this->password = $password;
    $this->database = $database;
}

function connect() 
{
    $this->db_handle = mysqli_connect(self::$server,$this->user_name,$this->password);

if(!$this->db_handle)
{
    die('Could not connect: ' . mysqli_error());
}

    mysqli_select_db($this->db_handle,$this->database);
    return $this->db_handle;
}

function disconnect() 
{
    mysqli_close($this->db_handle);
}

}
?>

class controller is here

<?php

class kontroler {

private $db_handle;
private $name;
private $surname;
private $password;
private $email;
private $id;
private $sql;

function _construct($db_handle="") 
{
    $this->db_handle = $db_handle;
}   

function delete_user($id="")
{
//$this->sql = "DELETE FROM UZYTKOWNICY WHERE ID_UZ=$id";

if(mysqli_query($this->db_handle,"DELETE FROM UZYTKOWNICY WHERE ID_UZ=$id")) 
    {
        echo "Record deleted successfully";
    }
         else 
    {
            die ('Error deleting record: ' . mysqli_error($this->db_handle));
    }
}


}

?>

and delete.php:

<?php
        global $db_handle;
        include 'kontroler.php';
        include 'connector.php';
        //require_once('kontroler.php');
        //require_once('connector.php');

        $connection = new connector("root","","proj");
        $db_handle = $connection->connect();
        $kontrol = new kontroler($db_handle);
        $kontrol->delete_user('$_POST[id]');
        $connection->disconnect();


    ?>      
user3308470
  • 107
  • 1
  • 3
  • 10
  • I changed the warning. It says 'null given' – user3308470 Dec 18 '14 at 02:39
  • Why is the server property static? Do yoy mean to make it `protected`? – Anthony Dec 18 '14 at 02:43
  • In mysql_query() you have to provide a variable which you actually assign to the query. – Hamza Zafeer Dec 18 '14 at 02:43
  • 1
    Why do you have so many different classes? Why not just make 1 single class? – Jhecht Dec 18 '14 at 02:44
  • Well, debug your code. `var_dump()` relevant variables every step of the way to ensure they're what you expect them to be. When you have found a discrepancy between expectation and reality, you've found your bug. – deceze Dec 18 '14 at 02:45
  • @Jhecht It's my first time with classes in PHP. I'm used to classes in java/c# where you make lots of classes. – user3308470 Dec 18 '14 at 02:48
  • @Anthony If function 'connect' is wrong it would throw 'could not connect' error i think – user3308470 Dec 18 '14 at 02:48
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource (or mysqli\_result), boolean given](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-or-mysqli-result-boole) – andrew Dec 18 '14 at 02:49
  • I think that $db_handle isn't passed properly from class 'connector 'to class 'kontroler' and so in 'kontroler' it's NULL and should be mysqli. But I don't know how to pass it properly. – user3308470 Dec 18 '14 at 02:50

1 Answers1

2

Assuming your connection class is in 'connection.php',

class connector {

    private $db_handle; //db handles are not strings. Unsure why you were setting this to a string by default.
    private $user_name;
    private $password;
    private $database;
    public static $server = "127.0.0.1"; 
    //I'd recommend against this, unless you know 100% that your live server will use 'localhost'

    function __construct($user_name="", $password="", $database="") 
    {
        $this->user_name = $user_name;
        $this->password = $password;
        $this->database = $database;
        //There really isn't a need to not have your connector here - but that's me being lazy.
        $this->db_handle = mysqli_connect(self::$server,$this->user_name, $this->password, $this->database);
        
    }
    
    function query($sql){
        //Do some checks, or if you use prepared statements, accept the prepared data array and parse it here.
        return mysqli_query($this->db_handle, $sql);
    }

    function __destruct() 
    {
        mysqli_close($this->db_handle);
    }

}

Then, in your page, do this:

include 'connector.php';

$db = new connector('user','pw','some_db');
$query = $db->query('DELETE FROM table WHERE user_id='.$_POST['something']);
//FOR THE LOVE OF GOD, DO NOT ACTUALLY DO THIS. I DID IT ONLY FOR THE SAKE OF BREVITY. USING RAW DATA SUBMITTED FROM THE USER
// IS DANGEROUS AND LEADS TO SQL INJECTIONS. *ALWAYS* SANITIZE YOUR DATA.

The reason is add the mysqli_connect and mysqli_close calls in the __construct and __destruct methods accordingly is because I am incredibly lazy and do not want to have to call a ->connect() method. And to be honest, my laziness has sometimes saved me a lot of work because I do not want to have to repeat myself over and over again (found out this is a coding philosophy called DRY - Don't Repeat Yourself).

Dharman
  • 30,962
  • 25
  • 85
  • 135
Jhecht
  • 4,407
  • 1
  • 26
  • 44
  • yeah. Some PHP frameworks will have you do something like that, but in home-brewed code it's just easier to have your objects all relate to the same sort of goal (in this case, a database connector/query caller) – Jhecht Dec 18 '14 at 03:31
  • 1
    What's wrong with my edit? Why do you roll back without any explanation? If I removed some technical details then please let me know, but from what I can see I only removed some noise which was making the answer more difficult to read. – Dharman Jan 21 '21 at 10:14
  • 2
    [Removing fluff is fine](https://meta.stackoverflow.com/q/260776/6296561). Refrain from adding it back in – Zoe Nov 13 '21 at 11:53