【问题标题】:Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\Final\data.php on line 14警告:mysqli_query() 期望参数 1 为 mysqli,在第 14 行的 C:\xampp\htdocs\Final\data.php 中给出 null
【发布时间】:2015-01-06 19:03:34
【问题描述】:

我需要帮助找出这是错误的原因

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

【问题讨论】:

  • 这意味着你的连接失败了!
  • 你有什么问题?
  • 你想对这篇文章说什么?请改进您的问题!

标签: php oop mysqli


【解决方案1】:

您的警告意味着您的连接失败!那是因为它从未被制造出来!

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

还有为什么__construct() 需要2x 下划线?因为这是一种神奇的方法!并引用手册:

注意: PHP 将所有以 __ 开头的函数名保留为神奇。建议您不要在 PHP 中使用带有 __ 的函数名,除非您想要一些记录在案的神奇功能。

有关魔术方法的更多信息,请参阅:http://php.net/manual/en/language.oop5.magic.php
有关构造函数的更多信息,请参阅:http://php.net/manual/en/language.oop5.decon.php

【讨论】:

    猜你喜欢
    • 2016-02-29
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多