<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>
}
?> Usage
include “./Api/datebase. "; 使用方法
include “./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
define('DB_HOST_NAME','localhost');
define('DB_HOST_USERNAME','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();