Data access and encapsulation using PHP

Source: Internet
Author: User

PHP Data access:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>

<body>
<?php

Build a connection, create a Connection object
$db = new Mysqli ("localhost", "root", "123", "MyDB");

Judging if there is an error
/*if (Mysqli_connect_error ())
{
echo "Connection failed! ";
Exit
}*/

Mysqli_connect_error () Die ("Connection Failed"): "";

Write SQL statements
$sql = "SELECT * from Info";

Executes the SQL statement, returning the result set object
$reslut = $db->query ($sql);

Reading data from the result set, returning an array
$attr = $reslut->fetch_all (); Reads all data, returns an indexed two-dimensional array

$attr = $reslut->fetch_array (); Reads the data that the current pointer points to, and returns an array that exists for the Index association

$attr = $reslut->fetch_assoc (); Returns an associative array

$attr = $reslut->fetch_row (); Returns an indexed array

$attr = $reslut->fetch_object (); Return object
/* $arr = array ();
while ($attr = $reslut->fetch_row ())
{
Array_push ($arr, $attr);
}

Var_dump ($arr); * *

?>

</body>

Encapsulation:

<?php
//Build a file that encapsulates the class DBDA.class.php

Class dbda//defines a class with the class name Dbda
{
Public $host = " localhost ";//4 a more commonly used parameter: server address
Public $uid =" root ";//user name
Public $PDW =" 666 ";//password
Public $dbname =" Toupiao ";// Database name

//encapsulation Method
//1. Method of returning a two-dimensional array
/**
* Give an SQL statement that returns the result of execution
* @param string $sql user-specified SQL statement
* @param int $ SQL user-given statement type, 0 for additions and deletions, 1 for the query. General query use more, let $type default value is 1. If you change the value of the $type.
* @return Array Returns the result of the query and, if it is a query, returns a two-dimensional array. If the additions and deletions are changed, return to $result.
*/
Function Query ($sql, $type =1)
{
//Build Connection object
$db = new Mysqli ("$this->host", "$this->uid", "$ THIS->PDW "," $this->dbname ");

//Execute SQL statement
$result = $db->query ("$sql");

//Fetch data from the result set object. Queries do one method alone, others do another.
if ($type ==1)//If it is a query
{
return $result->fetch_all ();//Returns a two-dimensional array of queries
}
else//If additions and deletions are changed
{
Return $ result;//return $result
}
}
}
?

Reference to interface:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>

<body>

<?php
Include ("DBDA.class.php");//introduce the encapsulated class to this page
$db = new Dbda ();//Create a new object
$sql = "SELECT * from Info";
Var_dump ($db->query ($sql));//The 2nd parameter is a query, because the default value is 1.
?>

</body>

Data access and encapsulation using PHP

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.