<code><span><span><?php</span></span></code>
/*
@author Dishy
데이터베이스 캡슐화
*/
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>
}
?> 使用方法
include “./Api/datebase.php”
define('DB_HOST_NAME','localhost'); '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
')->select() jciot_member
var_dump($a);
위에서는 작업 측면을 포함하여 간단한 데이터베이스 체인 작업의 예를 소개했습니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.