Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

oop - Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:xampphtdocsFinaldata.php on line 14

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;
        }
    }
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...