Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\Final\data.php on line 14

六月ゝ 毕业季﹏ 提交于 2021-01-20 13:59:35

问题


I need help to figure out why this is error

<?php
    class data
    {
        private $db;
        public $nama, $password, $alamat, $jk, $kodepos, $alasan, $email;
        function _construct($db)
        {
            $this->db = $db;
        }
        public function input_data()
        {
            $query = "INSERT INTO data (nama, password, alamat, jeniskelamin, kodepos, alasan, email)VALUES('$this->nama', '$this->password',
            '$this->alamat', '$this->jk', '$this->kodepos', '$this->alasan', '$this->email')";
            $insert = mysqli_query($this->db, $query);
            return $insert;
        }
        public function lihat_data()
        {
            $query = "SELECT * FROM data ORDER BY id";
            $view = mysqli_query($this->db, $query);
            return $view;
        }
    }
?>

回答1:


Your warning means that your connection has failed! And that's because it was never made!

function __construct($db) {
       //^^You need 2 underscores
    $this->db = $db;
}

Also why does __construct() needs 2x underscores? Because it's a magic method! And a quote from the manual:

Caution: PHP reserves all function names starting with __ as magical. It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.

For more information about magic methods see: http://php.net/manual/en/language.oop5.magic.php
For more information about the constructor see: http://php.net/manual/en/language.oop5.decon.php



来源:https://stackoverflow.com/questions/27805388/warning-mysqli-query-expects-parameter-1-to-be-mysqli-null-given-in-c-xampp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!