Idea: The constructor completes the database connection, adding and deleting a method, query a record a method, query multiple methods, SQL execution fails to return a hint, and hand over the SQL statement to facilitate error checking
Code:
<?
Class dbcontroll{
Private $links;
Private $sql;
Public function __construct ($host, $user, $password, $db) {
$con = mysql_connect ($host, $user, $password);
if (! $con) {
Echo ' Host connection information error! ';d ie;
}
$this->links = mysql_select_db ($db, $con);
if (! $this->links) {
Echo ' Database connection Error! ';d ie;
}
}
Delete and change
Public Function Query ($sql) {
$res = mysql_query ($sql);
if (! $res) {
echo ' SQL execution error '. $sql;
}else{
Echo ' execution succeeded! ';
}
}
Querying more than one piece of data
Public Function Getmore ($sql) {
$res = mysql_query ($sql);
if (! $res) {
echo ' SQL execution error '. $sql;
}else{
$arr = Array ();
while ($row = Mysql_fetch_row ($res)) {
Array_push ($arr, $row);
}
return $arr;
}
}
Querying a single piece of data
Public function Find ($sql) {
$res = mysql_query ($sql);
if (! $res) {
echo ' SQL execution error '. $sql;
}else{
while ($row = Mysql_fetch_row ($res)) {
return $row;
}
}
}
}
$sql = "SELECT * from login where id = 3";
$db = new Dbcontroll (' localhost ', ' root ', ' root ', ' login ');
$a = $db->find ($sql);
Var_dump ($a);
?>
:
PHP Object-oriented Exercise 2