duanhuo7441 2017-07-24 15:53
浏览 180
已采纳

从php中获取sqlite3数据库中的数据

I need some help with my PHP. I have a trouble with fetching the data from the database. I have hired a PHP developer who did not do his job properly that he have messed up the code which make it don't work so I need some help to fix the issue to get it working again.

When I try this:

//open the database File
$db = new SQLite3('myChannel.db');

if(!$db) 
{
  echo $db->lastErrorMsg();
} 
else 
{
   $channel_name = $_GET['channels'];

   $sql ="SELECT channel, title, start_date, stop_date, description FROM programs WHERE channel='$channel_name'";

   $results = $db->query($sql);

   while ($row = $results->fetchArray()) 
   {
     print_r($row);
   }

What happen with the code is it will not fetching the matched data from the database as it will not do anything. I think there is something wrong with the $sql variable.

What I'm expecting to do is I want to look for data in the database where I use the variable called $channel_name, then I want to fetch the matched data to output them in my PHP.

Can you please help me how I can fetch the matched data in the database?

  • 写回答

1条回答 默认 最新

  • dtxzwdl08169 2017-07-24 16:00
    关注

    Try this code based on the SQLite PHP docs

    class MyDB extends SQLite3 {
        function __construct() {
            $this->open('myChannel.db');
        }
    }
    $db = new MyDB();
    if (!$db) {
        echo $db->lastErrorMsg();
    } else {
        $channel_name = $_GET['channels'];
        $sql = "SELECT channel, title, start_date, stop_date, description FROM programs WHERE channel='{$channel_name}'";
        $results = $db->query($sql);
        while($row = $results->fetchArray(SQLITE3_ASSOC) ) {
            print_r($row);
        }
    }
    

    I changed a few things. I turned your database connection into a class, and I changed your while to include SQLITE3_ASSOC.

    Warning: OP's code and as a result this answer has code that is vulnerable to SQL Injection!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 请问有人知道怎么用工艺库里面的sdb文件通过virtuoso导出来library里面每个cell的symbol吗?
  • ¥20 海思 nnie 编译 报错
  • ¥50 决策面并仿真,要求有仿真结果图
  • ¥15 springboot接入微信支付SDK
  • ¥50 大区域的遥感影像匹配 怎么做啊
  • ¥15 求解答:pytorch跑yolov8神经网络受挫
  • ¥20 Js代码报错问题不知道怎么解决
  • ¥15 gojs 点击按钮node的position位置进行改变,再次点击回到原来的位置
  • ¥20 halcon 图像拼接
  • ¥15 webstorm上开发的vue3+vite5+typeScript打包时报错