PHP Object-oriented Exercise 2

Source: Internet
Author: User

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

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.