问题
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