Example of simple database chain operation

WBOY
Release: 2016-07-29 09:13:12
Original
1360 people have browsed it
<code><span><span><?php</span></span></code>
Copy after login

/*

@author Dishy
Database encapsulation
*/
class mysqldb{

<code><span>private</span><span>$host</span>;
<span>private</span><span>$username</span>;
<span>private</span><span>$password</span>;
<span>private</span><span>$dbname</span>;
<span>private</span><span>$sql</span>;
<span>private</span><span>$select</span> = <span>true</span>;
<span>private</span><span>$result</span>;
<span>private</span><span>$array</span>;

<span>public</span><span><span>function</span><span>__construct</span><span>(<span>$host</span>,<span>$username</span>,<span>$password</span>,<span>$dbname</span>)</span></span>{
    <span>$this</span>->host = <span>$host</span>;
    <span>$this</span>->username = <span>$username</span>;
    <span>$this</span>->password = <span>$password</span>;
    <span>$this</span>->dbname = <span>$dbname</span>;
    <span>$this</span>->dbconnect();

}
<span>/*
    数据库连接
*/</span><span>private</span><span><span>function</span><span>dbconnect</span><span>()</span></span>{
    mysql_connect(<span>$this</span>->host,<span>$this</span>->username,<span>$this</span>->password);
    mysql_select_db(<span>$this</span>->dbname);

}
<span>/*
*执行sql语句
*
**/</span><span>public</span><span><span>function</span><span>query</span><span>(<span>$sql</span>)</span></span>{
        <span>$this</span>->sql = <span>$sql</span>;
        <span>$this</span>->result = mysql_query(<span>$this</span>->sql);
        <span>return</span><span>$this</span>;
}
<span>/*
*
*$select = false时输出sql语句
*/</span><span>public</span><span><span>function</span><span>select</span><span>(<span>$select</span>= true)</span></span>{
    <span>$this</span>->select = <span>$select</span>;
    <span>if</span>(<span>$this</span>->select){
        <span>return</span><span>$this</span>->array =  <span>$this</span>->result();
    }<span>else</span>{
        <span>return</span><span>$this</span>->sql;
    }
}
<span>/**
*结果集
*/</span><span>private</span><span><span>function</span><span>result</span><span>()</span></span>{
    <span>$array</span> = mysql_fetch_array(<span>$this</span>->result);
    <span>return</span><span>$array</span>;
}
</span></span></code>
Copy after login

}

?>
Usageinclude “./Api/datebase.php”;
define ('DB_HOST_NAME','localhost');
define('DB_HOST_USERNAME','root');
define('DB_HOST_PASSWORD','root');
define('DB_HOST_DB','jciot');
$Jciot_db = new mysqldb(DB_HOST_NAME,DB_HOST_USERNAME,DB_HOST_PASSWORD,DB_HOST_DB);
$a = $Jciot_db->query('select * from jciot_member')->select();
var_dump($a);

?> ;`

The above has introduced an example of a simple database chain operation, including aspects of the operation. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!