zoukankan      html  css  js  c++  java
  • 数据访问类

    做一个数据访问类 文件保存时  文件名要和类名统一(DBDB.class.php)

    用过用数据访问的方式做增删改查,在别的电脑上或者服务器更换后,数据就无法访问。数据访问类就可以有效的改善这个问题

    如果地址之类的更改在这个类里面改就可以

    <?php
    	class DBDB{
    		public $host = "localhost";//服务器地址
    		public $uid = "root";//用户名
    		public $pwd = "123456";//数据库密码
    		public $dbname = "crud";//数据库名字
    		//$sql,$type=0(两个值)//type=0是查询   type=1增删
    		//如果是查询语句返回二维数组,如果是增删改返回true或false改
    		public function query($sql,$type=0){
    			$db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
    			$result = $db->query($sql);
    			if($type){
    				return $result;
    			}else{
    				return $result->fetch_all();
    			}
    		}
    	}
    

    加载这个类   查询的方法

    <body>
    	<table border="1" width="30%">
        	<tr>
            	<td>代号</td>
                <td>姓名</td>
                <td>性别</td>
                <td>生日</td>
            </tr>
    <?php
    	require_once "DBDB.class.php";//加载数据  
    	$db = new DBDB();//new一下DBDB类
    	$sql = "select * from info";  
    	$result = $db->query($sql); //type 不写就默认DBDB类里面的
    	foreach($result as $arr){
    		echo"<tr>
    			<td>{$arr[0]}</td>
                <td>{$arr[1]}</td>
                <td>{$arr[2]}</td>
                <td>{$arr[4]}</td>
    		</tr>";	
    	}
    ?>
        </table>
    </body>
    

     

    加载这个类   增加的方法

    <body>
    	<table border="1" width="30%">
        	<tr>
            	<td>代号</td>
                <td>姓名</td>
                <td>性别</td>
                <td>生日</td>
            </tr>
    <?php
    	require_once "DBDB.class.php";//加载数据  
    	$db = new DBDB();//new一下
    	$sql = "select * from info";  
    	$result = $db->query($sql); //type 不写就默认DBDB类里面的
    	foreach($result as $arr){
    		echo"<tr>
    			<td>{$arr[0]}</td>
                <td>{$arr[1]}</td>
                <td>{$arr[2]}</td>
                <td>{$arr[4]}</td>
    		</tr>";	
    	}
    	$sql = "insert into info values ('p098','傅山度',0,'n001','1989-4-3')";//添加数据
    	$arr = $db->query($sql,$type=1);//$type=1  如果不写会报错
    ?>
        </table>
    </body>
    

     

  • 相关阅读:
    网页工具KOBAS进行KEGG富集分析
    Novel LncRNA的实时定量PCR引物设计教程
    Annotated LncRNA的实时定量PCR引物设计教程
    GO分析-GOseq的使用教程
    转录因子预测-oPOSSUM 3.0的使用教程
    miRNA结合位点预测软件RNAhybrid的使用教程
    关于win10用户名设置为中文导致Rstudio画图报错的解决方法
    edgeR之配对检验分析差异基因的使用教程
    51nod 1051最大子矩阵和
    51nod最大字段和(1049 1254)
  • 原文地址:https://www.cnblogs.com/navyouth/p/8309381.html
Copyright © 2011-2022 走看看