Ask my little brother I wrote this db class, wrong, plug in the data

Source: Internet
Author: User
Ask me to write this DB class, where wrong, plug in the data
 Class db{
Private $hostname;
Private $username;
Private $password;
Private $select _db;
Private $con;
Private $Error;

Public function __construct ($hostname, $username, $password, $select _db) {
if (!empty ($hostname) &&!empty ($username) &&!empty ($select _db))//Check whether the parameter is empty, otherwise it is not assigned
{
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
$this->select_db = $select _db;
$this->con = mysql_connect ($this->hostname, $this->username, $this->password);
if (! $this->con) {
$this->error = die (' Could not Connect: '. mysql_error);
}
else{
mysql_select_db ($this->select_db, $this->con);
}
}
}
Public Function __destruct () {//End connection on exit
Mysql_close ($this->con);
}
Public Function Insert ($table, $body) {//Insert an array in table
$line 1 = implode (', ', $body);
$line 2 = Implode (', ', Array_keys ($body));
echo $sql = "INSERT into $table ($line 2) VALUES ($line 1)";
$result = mysql_query ($sql, $this->con);
if (! $result) {
Echo $this->error;
Echo ' 111 ';
}

}
Public Function Update ($table, $body) {

}
Public function Read ($table, $keyword) {

}
Public Function Delete ($table, $keyword) {

}
Public Function GetLastError () {//Return last error message
return $this->error;
}
}
?>

The home page is called
 
  Require (' DB.class.php ');
$DB = new DB (' localhost ', ' root ', ' ', ' dbtest ');
$line 1 = Array (
' AA ' + = ' AA ',
' BB ' = ' dd '
);
$DB->insert (' 1234 ', $line 1);
?>

echo $sql statement is insert INTO 1234 (AA,BB) VALUES (' AA ', ' DD ')


------Solution--------------------
Require (' DB.class.php ');
$DB = new DB (' localhost ', ' root ', ' ', ' dbtest ');
$line 1 = Array (
' aa ' = ' ' AA ',
' BB ' = ' dd '
);
$DB->insert (' 1234 ', $line 1);

------Solution--------------------
Your $line1 array is written in reverse.
The INSERT statement also has a format: INSERT INTO Tb_member set username = "Test", type = 1, Lastlogindt = Now (). Similar to the update style.
You can also look at the database class written by others, I feel very good: http://www.cnblogs.com/hooray/archive/2012/07/21/2603017.html
  • 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.