dongzhang7382 2013-05-14 20:47
浏览 75
已采纳

从函数运行同步mysqli查询

I'm tryng to get this code to work(probably it's impossible,but just to be sure I ask):

$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)));

and

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;
                    }
                }
            }
        }
    }
}
}

Is there a way to run them in this way? At this moment it says that $Hostname, $Username, $Password, $DatabaseName aren't defined (but they are, mainly beacuse the first mysqli function run),it's like it can't read the var that I have setted at the beginning of the file, before every condition or function, just after the session_start().
These are the errors(the line correspond to the reported code):

[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


Thanks

  • 写回答

1条回答 默认 最新

  • donglu8779 2013-05-14 20:54
    关注

    You have an immediate problem with the scope of variables. Read Variable scope and try

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

    or pass them as parameters

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

    and then call the function

    retrive_operators($Hostname, $Username, $Password, $DatabaseName);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器