PHP uses classes to make database connections

Source: Internet
Author: User

<?php

Class Dbda
{
Public $host = "localhost"; Server address
Public $uid = "root"; User name
Public $pwd = "123"; Password

Public $dbconnect; Connection object


Ways to manipulate databases
$SQL represents the SQL statement that needs to be executed
$type represents the type of SQL statement, 1 represents the query, 2 represents
$dbname represents the name of the database to manipulate

If the query returns a two-dimensional array
Returns TRUE or False if additions and deletions are changed
function Query ($sql, $type =1, $dbname = "Student")
{
Connecting objects
$this->dbconnect = new Mysqli ($this->host, $this->uid, $this->pwd, $dbname);
Judging if there is an error
if (!mysqli_connect_error ())
{
If the connection executes the SQL statement successfully
$result = $this->dbconnect->query ($sql);
Judging by the type of statement
if ($type ==1)
{
Returns a two-dimensional array if it is a query statement
return $result->fetch_all ();

}
Else
{
If it is a different statement, return Ture or False
return $result;
}
}
Else
{
Return "link Failed";
}
}
}


?>

PHP uses classes to make database connections

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.