简体   繁体   English

在PHP中从sqlite3数据库中获取数据

[英]Fetching the data from sqlite3 database in php

I need some help with my PHP. 我的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. 我雇用了一名PHP开发人员,由于他弄乱了代码使其无法正常工作,因此未正确完成工作,因此我需要一些帮助来解决该问题,以使其重新运行。

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. 我认为$ sql变量有问题。

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. 我希望做的是在数据库中查找名为$channel_name的变量中的数据,然后获取匹配的数据以将其输出到PHP中。

Can you please help me how I can fetch the matched data in the database? 您能帮我如何获取数据库中匹配的数据吗?

Try this code based on the SQLite PHP docs 根据SQLite PHP文档尝试此代码

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 . 我将您的数据库连接变成一个类,并且更改了您的时间以包括SQLITE3_ASSOC

Warning: OP's code and as a result this answer has code that is vulnerable to SQL Injection! 警告:OP的代码,因此,此答案包含的代码容易受到SQL注入的攻击!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM