我写了一些从数据库获取数据的代码,在mysql代码工作,在pdo它不工作

时间:2016-05-04 09:30:16

标签: php mysql pdo

这是mysql连接编码,工作正常

我用pdo连接编写的相同代码,它不工作我是php的新手请帮助我摆脱这个问题。 提前谢谢

<?php   
error_reporting(0);
 ini_set('max_execution_time', 600);
 require_once 'config.php';

 if(isset($_GET["nm_mask"]))
     $nm_mask = $_GET['nm_mask'];
 else 
     $nm_mask = "";
 if(isset($_GET["cd_mask"])) 
     $cd_mask = $_GET['cd_mask'];
 else
     $cd_mask = "";

 if(isset($_GET["func"])) 
     $func = $_GET['func'];
 else
     $func = "";

 $where = "WHERE 1=1";
 if($nm_mask!='')
     $where.= " AND Login LIKE '$nm_mask%'";
 if($cd_mask!='')
     $where.= " AND Manager_Login LIKE '$cd_mask%'";
  if($func!='')
     $where.= " AND Function LIKE '$func%'";

 $page = $_GET['page']; // get the requested page
 $limit = $_GET['rows']; // get how many rows we want to have into the grid
 $sidx = $_GET['sidx']; // get index row - i.e. user click to sort
 $sord = $_GET['sord']; // get the direction

 if(!$sidx) $sidx =1; // connect to the database
 $result = mysqli_query("SELECT COUNT(*) AS count FROM EmpMasterTB ".$where);
 $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
 $count = $row['count'];

 if( $count >0 ) { 
 $total_pages = ceil($count/$limit);
 } else { 
 $total_pages = 0; 
 } 
 if ($page > $total_pages) $page=$total_pages;
 if ($limit<0) $limit = 0; 
 $start = $limit*$page - $limit; // do not put $limit*($page - 1)
 if ($start<0) $start = 0;
 $SQL = "SELECT * from EmpMasterTB ". $where ." ORDER BY $sidx $sord LIMIT $start , $limit"; 
 $result = mysqli_query( $SQL ) or die("Couldn?t execute query.".mysqli_error());
 $responce->page = $page;
 $responce->total = $total_pages;
 $responce->records = $count; 
 $i=0;
 while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) { 
 $responce->rows[$i]['id']=$row[WeekNo].$row[Login];
 $responce->rows[$i]['cell']=array($row['WeekNo'],$row['WeekBeginning'],$row['SITE'],$row['Name'],$row['WFH'],$row['Login'],$row['Manager_Login'],$row['Lead'],$row['Cost_center'],$row['Business_Title'],$row['Function'],$row['Workgroup'],$row['Login_time'],$row['ROLE'],$row['Secondary_Skill'],$row['Weekoff'],""); $i++;
 } 
 echo json_encode($responce);

?>

PDO连接代码

<?php

error_reporting(0);
 ini_set('max_execution_time', 600);
 require_once 'config.php';

 if(isset($_GET["nm_mask"]))
     $nm_mask = $_GET['nm_mask'];
 else 
     $nm_mask = "";
 if(isset($_GET["cd_mask"])) 
     $cd_mask = $_GET['cd_mask'];
 else
     $cd_mask = "";

 if(isset($_GET["func"])) 
     $func = $_GET['func'];
 else
     $func = "";

 $where = "WHERE 1=1";
 if($nm_mask!='')
     $where.= " AND Login LIKE '$nm_mask%'";
 if($cd_mask!='')
     $where.= " AND Manager_Login LIKE '$cd_mask%'";
  if($func!='')
     $where.= " AND Function LIKE '$func%'";

 $page = $_GET['page']; // get the requested page
 $limit = $_GET['rows']; // get how many rows we want to have into the grid
 $sidx = $_GET['sidx']; // get index row - i.e. user click to sort
 $sord = $_GET['sord']; // get the direction

 if(!$sidx) $sidx =1; // connect to the database
 $count = $dbh->exec("SELECT COUNT(*) AS count FROM EmpMasterTB ".$where);


 if( $count >0 ) { 
 $total_pages = ceil($count/$limit);
 } else { 
 $total_pages = 0; 
 } 
 if ($page > $total_pages) $page=$total_pages;
 if ($limit<0) $limit = 0; 
 $start = $limit*$page - $limit; // do not put $limit*($page - 1)
 if ($start<0) $start = 0;
 $SQL = "SELECT * from EmpMasterTB ". $where ." ORDER BY $sidx $sord LIMIT $start , $limit";  /*** The SQL SELECT statement ***/
 $stmt = $dbh->query($SQL); /*** fetch into an PDOStatement object ***/

 $responce->page = $page;
 $responce->total = $total_pages;
 $responce->records = $count; 
 $i=0;

     /*** echo number of columns ***/
    $obj = $stmt->fetch(PDO::FETCH_OBJ);

    /*** loop over the object directly ***/
     { 
 $responce->rows[$i]['id']=$obj->WeekNo.$obj->Login;
 $responce->rows[$i]['cell']=array($obj->WeekNo ,$obj->WeekBeginning ,$obj->SITE ,$obj->Name ,$obj->WFH ,$obj->Login ,$obj->Manager_Login ,$obj->Lead ,$obj->Cost_center ,$obj->Business_Title ,$obj->Function ,$obj->Workgroup ,$obj->Login_time ,$obj->ROLE ,$obj->Secondary_Skill ,$obj->Weekoff ); $i++;
} 

 echo json_encode($ );

?>

1 个答案:

答案 0 :(得分:0)

你错过了循环,你没有在json_encode()中传递响应。 请检查我的以下代码:

 /*** echo number of columns ***/


    $objs = $stmt->fetch(PDO::FETCH_OBJ);

        /*** loop over the object directly ***/
foreach($objs as $obj)
         { 
     $responce->rows[$i]['id']=$obj->WeekNo.$obj->Login;
     $responce->rows[$i]['cell']=array($obj->WeekNo ,$obj->WeekBeginning ,$obj->SITE ,$obj->Name ,$obj->WFH ,$obj->Login ,$obj->Manager_Login ,$obj->Lead ,$obj->Cost_center ,$obj->Business_Title ,$obj->Function ,$obj->Workgroup ,$obj->Login_time ,$obj->ROLE ,$obj->Secondary_Skill ,$obj->Weekoff ); $i++;
    } 

     echo json_encode($responce );