How PHP makes a short, primitive database class

Source: Internet
Author: User

Class Dbda
{
Public $host = "localhost";
Public $uid = "root";
Public $pwd = "123";
Public $dbname = "MyDB";

/**
* Give an SQL statement, return the result of execution
* @param string $sql user-specified SQL statement
* @param int $type User-given statement type, 0 for adding or deleting, 1 for query
* @return Returns the result of the query, if the query returns a two-dimensional array, if it is deleted or not, returns TRUE or false
 */
function Query ($sql, $type =1)//type Default is 1, in the case of SQL statements are additions and deletions must not forget to write the type parameter 0
{
Connecting objects
$db = new Mysqli ($this->host, $this->uid, $this->pwd, $this->dbname);

Execute SQL statement
$reslut = $db->query ($sql);

Fetching data from the result set object
if ($type ==1)
{
return $reslut->fetch_all ();
}
Else
{
return $reslut;
}
}

 /**
* Give an SQL statement that returns the associated two-dimensional array
* @param string $sql user-specified SQL statement
* @param int $type User-given statement type, 0 for adding or deleting, 1 for query
* @return Returns the result of the query, if the query returns a two-dimensional array, if it is deleted or not, returns TRUE or false
 */
function Guanquery ($sql, $type =1)
{
Connecting objects
$db = new Mysqli ($this->host, $this->uid, $this->pwd, $this->dbname);

Execute SQL statement
$reslut = $db->query ($sql);

Fetch data
if ($type ==1)
{
$attr = Array ();
while ($a = $reslut->fetch_assoc ())
{
$attr [] = $a;
}

return $attr;
}
Else
{
return $reslut;
}
}
/**
* Give a SQL statement that returns a string
* @param string $sql user-specified SQL statement
* @param int $type User-given statement type, 0 for adding or deleting, 1 for query
* @return Returns the result of the query, if it is a query return string, if it is deleted and changed to return TRUE or false
 */
function strquery ($sql, $type =1)///Single data in case the strquery can give the result directly and is suitable for the aggregation function
{
Connecting objects
$db = new Mysqli ($this->host, $this->uid, $this->pwd, $this->dbname);

Execute SQL statement
$reslut = $db->query ($sql);

Fetch data
if ($type ==1)
{
$attr = $reslut->fetch_all ();
$str = "";
foreach ($attr as $v)
{
$str. = Implode ("^", $v); string concatenation
$str. = "|";
}
return substr ($str, 0,strlen ($STR)-1); Get rid of the last "|"
}
Else
{
return $reslut;
}
}
}

How PHP makes a short, primitive database class

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.