Small white first back to ask for advice, PHP connection to MySQL with the object-oriented style error, the code is as follows

Source: Internet
Author: User
Small white first post for advice, PHP connection MySQL with object-oriented style error, the code is as follows
Little Brother student party, a recent obsession with PHP programming, have encountered problems to find out the bad habits, code description as follows, the gods do not spray ...


Class sqlhelper{
Public $host = ' loaclhost ';
Public $user = ' root ';
Public $password = ' root ';
Public $database = ' empmanage ';
Public $mysqli;

Public Function __construct () {
$this->mysqli=new mysqli (' localhost ', ' root ', ' root ', ' empmanage ');
$this->mysqli=new mysqli ($this->host, $this->user, $this->password, $this->databse);//This connection is not. Whether it is to change private to public, or add static to access with self.

if ($this->mysqli->connect_error) {
Die (' failed to connect d '). $this->mysqli->connect_error);
}
$this->mysqli->query (' Set names UTF8 ');
}

Public Function Execute_dql ($sql) {
$result = $this->mysqli->query ($sql) or Die (' Operation 1 failed '. $this->mysqli->error);//query statement, return data
return ($result);
}


The problem is in red code, when used with $this->mysqli=new mysqli ($this->host, $this->user, $this->password, $this->databse) When the interview will be error, said: The connection lost D failed, no such host; This code no matter how to change it, I tried to use self:: Access static variable (in the member property added static), try to change private to public, will eventually fail and then report another error: Operation 1 failed, No database selected; Finally I found that only the inside of the code into the line, is the red above the line, the younger brother here do not know where the error, technology is not clearance, the great God.
------to solve the idea----------------------
Note the spelling of the word
Public $host = ' localhost';


$this->mysqli=new mysqli ($this->host, $this->user, $this->password, $this Database);
------to solve the idea----------------------
Spelling error ... Database
------to solve the idea----------------------
The next time the landlord encountered this problem can be the whole phrase to lose out to see what is not assigned to know it?
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.