duanhuo7441
2017-07-24 15:53
浏览 102

从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?

图片转代码服务由CSDN问答提供 功能建议

我需要一些PHP帮助。 我在从数据库中获取数据时遇到了麻烦。 我聘请了一个PHP开发人员,他没有正确地完成他的工作,他搞砸了代码,这使得它不起作用所以我需要一些帮助来解决问题,让它再次运行。

当我尝试这个时:

  //打开数据库文件
 $ 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); 
} 
   
 
 

代码会发生什么 不从数据库中提取匹配的数据,因为它不会做任何事情。 我认为$ sql变量有问题。

我想要做的是我想在数据库中查找我使用名为 $的变量的数据 channel_name ,然后我想获取匹配的数据,以便在我的PHP中输出它们。

你能帮我解决一下如何在数据库中获取匹配的数据吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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!

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题