douhanxujiuji6098 2014-12-18 02:31
浏览 15
已采纳

too long

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();


    ?>      
  • 写回答

1条回答 默认 最新

  • dragon321723 2014-12-18 03:00
    关注

    This is a very brief answer, but I hope this helps. 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). Hopefully this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line