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