0

我试图让这段代码工作(可能是不可能的,但只是为了确保我问):

$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


谢谢

4

1 回答 1

0

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

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

或将它们作为参数传递

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

然后调用函数

retrive_operators($Hostname, $Username, $Password, $DatabaseName);
于 2013-05-14T20:54:07.053 回答