Rumah > pembangunan bahagian belakang > tutorial php > 简单的数据库链式操作的例子

简单的数据库链式操作的例子

WBOY
Lepaskan: 2016-07-29 09:13:12
asal
1384 orang telah melayarinya
<code><span><span><?php </span></span></span></code>
Salin selepas log masuk

/* 

@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>;
}
</code>
Salin selepas log masuk

}

?> 
使用方法 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')->select(); 
var_dump($a);

?>`

以上就介绍了简单的数据库链式操作的例子,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan