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