Problems with MySQL database classes

Source: Internet
Author: User
Mysql

This is my MySQL class, inherited to the DB abstract class, where the abstract method of DB is implemented in MySQL,
DB Class:
Abstract class db{
Connecting to a database
Abstract public Function Connect ($h, $u, $p);
Send Query
Abstract Public Function query ($sql);
Querying multiple rows of data
Abstract public Function getAll ($sql);
Querying single-line data
Abstract public Function GetOne ($sql);

Querying individual 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 configuration information for a database
protected function __construct () {
$this->conf=conf::getins ();
$this->select_db ($this->conf->db);
$this->connect ($this->conf->host, $this->conf->user, $this->conf->pwd);//Display is empty
$this->setchar ($this->conf->char);
}
Public Function __destruct () {

}
Use singleton mode to allow only new once
public static function Getins () {
if (self:: $ins ===null) {
Self:: $ins =new self ();
}
Return self:: $ins;
}
Connection, exception thrown when connection fails
Public function Connect ($h, $u, $p) {
Didn't come to 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 ' failure
';
Var_dump ($this->conn);
Echo '
';
Var_dump ($this->conf);


}
return $rs;
}
Automatic calculation
Public Function Autoexecute ($arr, $table, $mode = ' Insert ', $where = ' 1 limit 1 ') {
if (!is_array ($arr)) {
return false;
}//updating data in a table
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 INTO ' $table. ' ('. Implode (', ', Array_keys ($arr)) ';
$sql. = ' VALUES (\ ';
$sql. =implode ("', '", Array_values ($arr));
$sql. = ' \ ') ';
return $this->query ($sql);
}
Take out all the qualifying rows of data in the table
Public Function GetAll ($sql) {
$rs = $this->query ($sql);
$list =array ();
while ($row =mysql_fetch_assoc ($rs)) {
$list []= $row;
}
return $list;
}
Take out a row of data that matches the criteria
Public Function GetRow ($sql) {
$rs = $this->query ($sql);
Return Mysql_fetch_assoc ($RS);
}
Take out a data
Public Function GetOne ($sql) {
$rs = $this->query ($sql);
$row =mysql_fetch_assoc ($RS);
return $row [0];
}
Take out 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 it appears: Warning:mysql_query (): supplied argument is not a valid Mysql-link resource
A lot of bugs were detected and the routines didn't go
Public function Connect ($h, $u, $p) {
Didn't come to this step.
$this->conn=mysql_connect ($h, $u, $p);
}
I can't think of a reason.


Reply to discussion (solution)

Read configuration information for a database
protected function __construct () {

You specify the constructor as a protected 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.