从函数运行同步mysqli查询

时间:2013-05-14 20:47:49

标签: php mysqli synchronous

我试着让这段代码起作用(可能这是不可能的,但我确定要问):

$query = "SELECT `id`,`department_name`,`operator_id`,`title`,`priority`,`created_time`,`ticket_status`  FROM ".$SupportTicketsTable." WHERE `user_id`=".$_SESSION['id'] ;
    $prepared = $stmt->prepare($query);
    if($prepared){
        if($stmt->execute()){
            $stmt->store_result();
            $result = $stmt->bind_result($id, $dname, $opid, $title, $priority, $dat, $status);
            $departments=array('response'=>'ret','tickets'=>array());
            $departments['tickets']['user']=array();
            if($stmt->num_rows>0){
                while (mysqli_stmt_fetch($stmt)) {
                    switch ($status) {
                        case 0:
                            $stat='Closed';
                            break;
                        case 1:
                            $stat='Open';
                            break;
                        case 2:
                            $stat='Under Assignment';
                            break;
                    }
                    switch ($priority) {
                        case 0:
                            $stat='Low';
                            break;
                        case 1:
                            $stat='Medium';
                            break;
                        case 2:
                            $stat='High';
                            break;
                        case 3:
                            $stat='Urgent';
                            break;
                    }
                    if(isset($_SESSION['operators'][$opid]))
                        $opname=$_SESSION['operators'][$opid];
                    else{
                        retrive_operators();
                        $opname=$_SESSION['operators'][$opid];
                    }
                    $departments['tickets']['user'][]=array('id'=>$id,'dname'=>$dname,'opname'=>$opname,'title'=>$title,'priority'=>$priority,'date'=>$dat,'status'=>$status,"action"=>'<div class="btn-group"><button class="btn btn-info editdep" value="'.$id.'"><i class="icon-edit"></i></button><button class="btn btn-danger remdep" value="'.$id.'"><i class="icon-remove"></i></button></div>');
                }
            }
        }
        else
            echo json_encode(array(0=>mysqli_stmt_error($stmt)));
    }
    else
        echo json_encode(array(0=>mysqli_stmt_error($stmt)));

function retrive_operators(){
if(isset($_SESSION['name'])){
    $mysqli = new mysqli($Hostname, $Username, $Password, $DatabaseName);
    $stmt = $mysqli->stmt_init();
    if($stmt){
        $query = "SELECT `id`,`name`  FROM ".$SupportUserTable." WHERE status='1' OR status='2'" ;
        $prepared = $stmt->prepare($query);
        if($prepared){
            if($stmt->execute()){
                $stmt->store_result();
                $result = $stmt->bind_result($id, $name);
                if($stmt->num_rows>0){
                    $_SESSION['operators']=array();
                    while (mysqli_stmt_fetch($stmt)) {
                        $_SESSION['operators'][$id]=$name;
                    }
                }
            }
        }
    }
}
}

有没有办法以这种方式运行它们?此时它表示$Hostname, $Username, $Password, $DatabaseName未定义(但它们主要是因为第一个mysqli函数运行),它就像它无法读取我在文件开头设置的var,之前每个条件或功能,就在session_start()之后 这些是错误(该行对应于报告的代码):

[14-May-2013 20:34:35 UTC] PHP Notice:  Undefined variable: Hostname in C:\xampp\htdocs\php\function.php on line 983

[14-May-2013 20:34:35 UTC] PHP Notice:  Undefined variable: Username in C:\xampp\htdocs\php\function.php on line 983

[14-May-2013 20:34:35 UTC] PHP Notice:  Undefined variable: Password in C:\xampp\htdocs\php\function.php on line 983

[14-May-2013 20:34:35 UTC] PHP Notice:  Undefined variable: DatabaseName in C:\xampp\htdocs\php\function.php on line 983

[14-May-2013 20:34:36 UTC] PHP Notice:  Undefined variable: SupportUserTable in C:\xampp\htdocs\php\function.php on line 986

[14-May-2013 20:34:36 UTC] PHP Notice:  Undefined index: operators in C:\xampp\htdocs\php\function.php on line 823


感谢

1 个答案:

答案 0 :(得分:0)

您对变量范围有直接的问题。阅读 Variable scope 并尝试

function retrive_operators(){
    global $Hostname, $Username, $Password, $DatabaseName;
    ...

或将它们作为参数传递

function retrive_operators($Hostname, $Username, $Password, $DatabaseName){
....

然后调用函数

retrive_operators($Hostname, $Username, $Password, $DatabaseName);