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!

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

报告相同问题?