网络营销电子商务研究中心  
How to buy the best prescription safety glasses in Canada? Let's study!
Go Back   网络营销电子商务研究中心 > 网站设计 > PHP
User Name
Password
 
FAQ Members List Calendar Cheap Glasses Mark Forums Read

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #3   IP: 180.125.23.38
Old 2016-03-02, 07:40 AM
Kaiden Kaiden is offline
初级会员
 
Join Date: 2011-06-08
Posts: 3
Kaiden 现在声名狼藉
Default

测试结果:



7.6 测试删除操作

测试代码:

Code:
if(trim($keywords[0] == "删除")){
    $delete_sql="DELETE FROM test_mysql WHERE from_user='$fromUsername'";
    $res = _delete_data($delete_sql);
    if($res == 1){
        $contentStr = "删除成功";
    }elseif($res == 0){
        $contentStr = "删除失败";
    }
}
测试结果:



与微信的交互测试成功。

八、PHP Mysqli 扩展,封装成类

将Mysqli 扩展封装成类使用,代码如下:

Code:
<?php

require_once 'includes/configure.php';

class MySQLi_BAE{

    private $mysqli;
    private $host;
    private $user;
    private $password;
    private $port;
    private $database;

    //在类之外访问私有变量时使用
    function __get($property_name){
        if(isset($this->$property_name)){
            return($this->$property_name);
        }else{
            return(NULL);
        }    
    }

    function __set($property_name, $value){
        $this->$property_name=$value;
    }

    function __construct(){

        /*从平台获取查询要连接的数据库名称*/
        $this->database = MYSQLNAME;

        /*从环境变量里取出数据库连接需要的参数*/
        $this->host = getenv('HTTP_BAE_ENV_ADDR_SQL_IP');
        $this->user = getenv('HTTP_BAE_ENV_AK');
        $this->password = getenv('HTTP_BAE_ENV_SK');
        $this->port = getenv('HTTP_BAE_ENV_ADDR_SQL_PORT');

        $this->mysqli = new mysqli($this->host, $this->user, $this->password, $this->database, $this->port);
        if($this->mysqli->connect_error){
            die("Connect Server Failed:".$this->mysqli->error);
        }
        
        $this->mysqli->query("set names utf8");
    }

    //dql statement
    function execute_dql($query){
        
        $res = $this->mysqli->query($query) or die("操作失败".$this->mysqli->error);
        return $res;
        
        //$this->mysqli->close();
    }

    //dml statement
    function execute_dml($query){
        
        $res = $this->mysqli->query($query) or die("操作失败".$this->mysqli->error);
        
        if(!$res){
            return 0;//失败
        }else{
            if($this->mysqli->affected_rows > 0){
                return 1;//执行成功
            }else{
                return 2;//没有行受影响
            }
        }
    
        //$this->mysqli->close();
    }
}
?>
九、测试类的使用

9.1 测试DML操作

测试代码:

Code:
<?php

require_once "MySQLi_BAE.class.php";

$mysqli_BAE=new MySQLi_BAE();


//**************dml*******************
$sql="insert into test_mysql (from_user, account, password, update_time) values('David','860510', 'abcabc', '2013-09-27 17:14:28')";

//$sql="update test_mysql set account = 860512 where account = 860510";

//$sql="delete from test_mysql where account = 860512";

$res=$mysqli_BAE->execute_dml($sql);

if($res==0){
    echo "执行失败";
}elseif($res==1){
    echo "执行成功";
}else{
    echo "没有行数影响";
}
?>
测试结果:



9.2 测试DQL操作

测试代码:

Code:
<?php

require_once "MySQLi_BAE.class.php";

$mysqli_BAE=new MySQLi_BAE();

//**************dql******************
$sql="select * from test_mysql";

$res=$mysqli_BAE->execute_dql($sql);

while($row=$res->fetch_row()){
    
    foreach($row as $key=>$val){
        echo "$val--";
    }
    echo '<br/>';
}

$res->free();
?>
测试结果:



十、实现与微信的交互(Mysqli 扩展)

10.1 前置操作

A. 引入MySQLi_BAE.class.php 文件

Code:
//引入数据库函数文件
require_once "MySQLi_BAE.class.php";
B. 实例化对象
Code:
public function __construct()
{
    $this->mysqli_BAE=new MySQLi_BAE();
}
10.2 测试插入操作

测试代码:

Code:
$insert_sql="INSERT INTO test_mysql(from_user, account, password, update_time) VALUES('$fromUsername','$keywords[1]','$keywords[2]','$nowtime')";
$res = $this->mysqli_BAE->execute_dml($insert_sql);
测试结果:



10.3 测试查询操作

测试代码:

Code:
$select_sql="SELECT * FROM test_mysql WHERE from_user='$fromUsername'";
$select_res=$this->mysqli_BAE->execute_dql($select_sql);
$rows=$select_res->fetch_array(MYSQLI_ASSOC);
测试结果:



10.4 测试更新操作

测试代码:

Code:
$update_sql="UPDATE test_mysql SET password='$new_password' WHERE from_user='$fromUsername'"; 
$res = $this->mysqli_BAE->execute_dml($update_sql);
测试结果:



10.5 测试删除操作

测试代码:

Code:
$delete_sql="DELETE FROM test_mysql WHERE from_user='$fromUsername'";
$res = $this->mysqli_BAE->execute_dml($delete_sql);
测试结果:



与微信交互测试成功。
Reply With Quote
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Prescription-glasses.com offers prescription glasses online at discount prices.
All times are GMT +8. The time now is 02:49 AM.


Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.