php - Run synchronous mysqli query from function -
i'm tryng code work(probably it's impossible,but sure ask):
$query = "select `id`,`department_name`,`operator_id`,`title`,`priority`,`created_time`,`ticket_status` ".$supportticketstable." `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` ".$supportusertable." 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 way run them in way? @ moment says $hostname, $username, $password, $databasename
aren't defined (but are, beacuse first mysqli function run),it's can't read var have setted @ beginning of file, before every condition or function, after session_start()
.
these errors(the line correspond 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
you have immediate problem scope of variables. read variable scope , try
function retrive_operators(){ global $hostname, $username, $password, $databasename; ...
or pass them parameters
function retrive_operators($hostname, $username, $password, $databasename){ ....
and call function
retrive_operators($hostname, $username, $password, $databasename);
Comments
Post a Comment