Problems in the mysql database class-php Tutorial

Source: Internet
Author: User
Mysql database problems

This is my mysql class, inherited to the db abstract class, where db abstract methods are implemented in mysql,
Db Class:
Abstract class db {
// Connect to the database
Abstract public function connect ($ h, $ u, $ p );
// Send query
Abstract public function query ($ SQL );
// Query multiple rows of data
Abstract public function getAll ($ SQL );
// Query a single row of data
Abstract public function getOne ($ SQL );

// Query a single data
Abstract public function getRow ($ SQL );
Abstract public function autoExecute ($ arr, $ table, $ mode = 'insert', $ where = '1 limit 1 ');
}
Class mysql extends db {
Private static $ ins = null;
Private $ conn = null;
Private $ conf = array ();
// Read database configuration information
Protected function _ construct (){
$ This-> conf = conf: getIns ();
$ This-> select_db ($ this-> conf-> db );
$ This-> connect ($ this-> conf-> host, $ this-> conf-> user, $ this-> conf-> pwd); // The display is empty.
$ This-> setChar ($ this-> conf-> char );
}
Public function _ destruct (){

}
// In Singleton mode, only new is allowed once.
Public static function getIns (){
If (self ::$ ins === null ){
Self: $ ins = new self ();
}
Return self: $ ins;
}
// Connection. An exception is thrown when the connection fails.
Public function connect ($ h, $ u, $ p ){
// You have not taken this step.
$ This-> conn = mysql_connect ($ h, $ u, $ p );
}
Protected function select_db ($ db ){
$ SQL = 'use'. $ db;
$ This-> query ($ SQL );
}

Protected function setChar ($ char ){
$ SQL = 'set names'. $ char;
Return $ this-> query ($ SQL );
}
// Send data
Public function query ($ SQL ){
/* If ($ this-> conf-> debug ){
Log: write ($ SQL );
}*/
/* Var_dump ($ SQL );
Exit ;*/
$ Rs = mysql_query ($ SQL, $ this-> conn );
/* If (! Rs ){
Log: write ($ this-> error ());
}*/
If (! $ Rs ){
Echo 'failed
';
Var_dump ($ this-> conn );
Echo'
';
Var_dump ($ this-> conf );


}
Return $ rs;
}
// Automatically calculate
Public function autoExecute ($ arr, $ table, $ mode = 'insert', $ where = '1 limit 1 '){
If (! Is_array ($ arr )){
Return false;
} // Update table data
If ($ mode = 'update '){
$ SQL = 'update'. $ table. 'set ';
Foreach ($ arr as $ k =>$ v ){
$ SQL. = $ k. "= '". $ v ."',";
}
$ SQL = rtrim ($ SQL ,',');
$ SQL. = $ where;
Return $ this-> query ($ SQL );
}
$ SQL = 'insert'. $ table. '('. implode (',', array_keys ($ arr )).')';
$ SQL. = 'values (\'';
$ SQL. = implode ("','", array_values ($ arr ));
$ SQL. = '\')';
Return $ this-> query ($ SQL );
}
// Retrieve all qualified multi-row data in the table
Public function getAll ($ SQL ){
$ Rs = $ this-> query ($ SQL );
$ List = array ();
While ($ row = mysql_fetch_assoc ($ rs )){
$ List [] = $ row;
}
Return $ list;
}
// Retrieve a row of data that meets the condition
Public function getRow ($ SQL ){
$ Rs = $ this-> query ($ SQL );
Return mysql_fetch_assoc ($ rs );
}
// Retrieve a data
Public function getOne ($ SQL ){
$ Rs = $ this-> query ($ SQL );
$ Row = mysql_fetch_assoc ($ rs );
Return $ row [0];
}
// Retrieve the affected data
Public function affected_rows (){
Return mysql_affected_rows ($ this-> conn );
}
// Insert an id
Public function insert_id (){
Return mysql_insert_id ($ this-> conn );
}
}

Why is: Warning: mysql_query (): supplied argument is not a valid MySQL-Link resource?
A lot of errors have been found, and the program has not been reached
Public function connect ($ h, $ u, $ p ){
// You have not taken this step.
$ This-> conn = mysql_connect ($ h, $ u, $ p );
}
No reason


Reply to discussion (solution)

// Read database configuration information
Protected function _ construct (){

You specify the constructor as the protection mode.
How can it be executed?

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.