Maison > développement back-end > tutoriel php > 简单的数据库链式操作的例子

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

WBOY
Libérer: 2016-07-29 09:13:12
original
1375 Les gens l'ont consulté
<code><span><span><?php </span></span></span></code>
Copier après la connexion

/* 

@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>
Copier après la connexion

}

?> 
使用方法 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教程有兴趣的朋友有所帮助。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal