将SQLi查询转换为SQLITE3以进行jQuery自动填充

时间:2017-10-22 07:15:57

标签: jquery sqlite

这是我用来处理jQuery自动填充的代码。

$connection = mysqli_connect("localhost","username","password","database"); 
   if (mysqli_connect_errno())
     {
     echo "Failed to connect to MySQL: " . mysqli_connect_error();
     }
       $sql = "SELECT * FROM patientemr WHERE regno LIKE '%".$_GET['term']."%' LIMIT 10";
       $result = mysqli_query($connection,$sql);
       //print_r ($result);
       $arr = array();
       while($row = mysqli_fetch_array($result)){
       $arr[] = array(
            'label' => $row['regno'],
            'value' => $row['regno'],
            'ptname' => $row['ptname'],
            'age' => $row['age'],
            'sex' => $row['sex'],
            'address' => $row['address'],
    );  
   }
       echo json_encode($arr);
       mysqli_close($connection);

现在我正在尝试为SQLITE3迁移这些代码。我尝试过使用SQLITE手册但没有工作。这样做的正确方法是什么?我试过的是 -

  $db = new SQLite3('database.db');
        $sql = "SELECT * FROM patientemr WHERE regno LIKE '%".$_GET['term']."%' LIMIT 10";
        $result = $db->query($sql);
        $arr = array();
         while($row = $result->fetchArray(SQLITE3_ASSOC)){
$arr[] = array(
                'label' => $row['regno'],
                'value' => $row['regno'],
                'ptname' => $row['ptname'],
                'age' => $row['age'],
                'sex' => $row['sex'],
                'address' => $row['address'],
        );           

          }
       echo json_encode($arr);

1 个答案:

答案 0 :(得分:0)

这解决了这个问题。把它放在这里供参考。

<?php
   $db = new SQLite3('database.db');
        $sql = "SELECT * FROM patientemr WHERE regno LIKE '%".$_GET['term']."%' LIMIT 10";
        $result = $db->query($sql);
        $row = array(); 
         $i = 0; 
         while($res = $result->fetchArray(SQLITE3_ASSOC)){ 
if(!isset($res['regno'])) continue; 
              $row[$i]['label'] = $res['regno'];
              $row[$i]['value'] = $res['regno'];
              $row[$i]['ptname'] = $res['ptname'];
              $row[$i]['age'] = $res['age'];
              $row[$i]['sex'] = $res['sex'];
              $row[$i]['address'] = $res['address'];
              $i++; 
          }
       echo json_encode($row);
?>