【问题标题】:PHP mysqli_query() expects parameter 1 to be mysqli, null given inPHP mysqli_query() 期望参数 1 为 mysqli,在
【发布时间】:2014-12-18 02:31:47
【问题描述】:

我有 2 个类,控制器和连接器。它们由 delete.php 使用。我说错了

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

有人知道我在这里犯错了吗?对我来说,连接的链接似乎被错误地传递给了某个地方的控制器类。或者退货不能按我的意愿工作。 在我不得不上课之前,这一切都奏效了……

类连接器在这里:

<?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);
}

}
?>

类控制器在这里

<?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));
    }
}


}

?>

并删除.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();


    ?>      

【问题讨论】:

  • 我更改了警告。它说'null given'
  • 为什么服务器属性是静态的?你的意思是让它protected
  • 在 mysql_query() 中你必须提供一个你实际分配给查询的变量。
  • 为什么有这么多不同的课程?为什么不只做 1 节课?
  • 好吧,调试你的代码。 var_dump() 相关变量的每一步,以确保它们符合您的期望。当您发现期望与现实之间存在差异时,您就找到了错误。

标签: php mysqli


【解决方案1】:

假设你的连接类在'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);
    }

}

然后,在您的页面中执行以下操作:

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.

原因是在__construct__destruct 方法中相应地添加mysqli_connectmysqli_close 调用是因为我非常懒惰并且不想调用-&gt;connect() 方法。老实说,我的懒惰有时为我节省了很多工作,因为我不想一遍又一遍地重复自己(发现这是一种叫做 DRY 的编码哲学 - 不要重复自己)。

【讨论】:

  • 是的。一些 PHP 框架会让你做类似的事情,但在自制代码中,让你的对象都与同一种目标相关(在这种情况下,数据库连接器/查询调用者)更容易
  • 我的编辑有什么问题?为什么不解释就回滚?如果我删除了一些技术细节,请告诉我,但据我所知,我只删除了一些使答案更难阅读的噪音。
  • Removing fluff is fine。避免将其添加回
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-08
  • 2013-09-26
相关资源
最近更新 更多