商会资讯

 找回密码
 立即注册

QQ登录

只需一步,快速开始

用新浪微博连接

一步搞定

搜索
热搜: 活动 交友 discuz

简单的PHP调用MYSQL存储过程的类

已有 2531 次阅读2009-12-8 20:42 |个人分类:php

<?php
/**
* created by david yeung 2007-8-31
* an easy way to connect mysql using php
*/

//define connection constant

define('my_host','localhost');
define('my_user','webuser');
define('my_pass','********');
define('my_db','test');
define('my_port','3307');
//for linux add this field

define('my_socket','/tmp/mysql_sock');

//php interface to mysqli

class db_mysqli{

    private$mysqli;
    private$row;
    private$count;
    private$affected_row_sql;
    private$affected_row_sp;
    //construct function

    publicfunction db_mysqli(){
    //check system is nt or linux

        if(php_os=='winnt'){
            $this->mysqli =new mysqli(my_host, my_user, my_pass, my_db, my_port);
        }else
        {
           $this->mysqli =new mysqli(my_host, my_user, my_pass, my_db, my_port,my_socket)
;
        }
        if(mysqli_connect_errno()){
            die("connect failed:".mysqli_connect_error());
        }
        $this->row =null;
        $this->count= 0;
    }
 //query sql text

    publicfunction call_sql($sql){
        $this->mysqli->query($sql);
        $this->affected_row_sql =$this->mysqli->affected_rows;

    }
 //query store procedure text

    publicfunction call_sp($query){
        $this->mysqli->multi_query($query);
        do{
           if($result=$this->mysqli->use_result()){
               while($row=$result->fetch_row()){
                  $this->row[$this->count++]=$row;
               }
               $result->close();
           }
        }while($this->mysqli->next_result());
        $this->affected_row_sp =$this->mysqli->affected_rows;

    }
    //close connection and free memory

    publicfunction close_sp(){
        $this->mysqli->close();
    }
    //get query result

    publicfunction get_rows(){
        return$this->row;
    }
    //get rows count

    publicfunction get_count(){
        return$this->count;
    }
    //get affected rows

    publicfunction get_affected_rows($str){
        switch($str){
           //get affected rows from sql text

           case'sql':
               return$this->affected_row_sql;
               break;
           //get affected rows from store procedure text

           case'sp':
               return$this->affected_row_sp;
               break;
           default:
               returnfalse;
               break;
      }
    }
}
//new a instant of this db class

$mysqli=new db_mysqli();
$query='call sp_test();';
$mysqli->call_sp($query);
$mysqli->close_sp();
print'<pre>';
print_r($mysqli->get_rows());
print'</pre>';
?>



测试结果:

mysql> delimiter ||
mysql>create procedure sp_test()
    -> begin
    ->select*from auto_t;
    ->end||
query ok, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql> call sp_test();
+----+---------+
| id | name |
+----+---------+
| 1 | 3 |
| 2 | 5 |
| 3 | sdflsjf |
+----+---------+
3 rows inset(0.00 sec)

query ok, 0 rows affected (0.00 sec)

php 代码:

array
(
    [0]=> array
        (
            [0]=> 1
            [1]=> 3
        )

    [1]=> array
        (
            [0]=> 2
            [1]=> 5
        )

    [2]=> array
        (
            [0]=> 3
            [1]=> sdflsjf
        )

)

收藏 分享邀请 转发到微博 举报

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

回顶部