PHP PDO选择带有where条件和Order by子句的查询


php pdo select query with where condition and order by clause

我需要从我的数据库中选择YouTube源。

我在 PHP PDO 中创建我的代码。

为此,我提出了这样的条件...

但这不起作用,甚至不显示任何错误或不显示视频。

当我从下面删除条件时,选择查询其正常工作....

请建议如何使用 PHP PDO 准备语句将此条件放入选择查询中。

谢谢。。。

下面是我的代码选择..

<?php
$youtubesource = "youtubesource <>  ''";
$youtube = $database->getRows("SELECT youtubesource FROM news where youtubesource = :youtubesource ORDER BY id DESC LIMIT 0,1",array(':youtubesource'=>$youtubesource));                        
?>

下面是我的 getrows 函数

 public function getRows($query, $params=array()){
            try{ 
                $stmt = $this->datab->prepare($query); 
                $stmt->execute($params);
                return $stmt->fetchAll();       
                }catch(PDOException $e){
                throw new Exception($e->getMessage());
            }       
        }

如果我体面地分析您的查询,这就是我看到的:

SELECT youtubesource 
FROM news 
WHERE youtubesource = :youtubesource     
ORDER BY id DESC 
LIMIT 0,1

youtubesource <> '' :youtubesource 的情况下,最终查询将是:

SELECT youtubesource 
FROM news 
WHERE youtubesource = 'youtubesource <> '''   
ORDER BY id DESC
LIMIT 0,1

youtubesource <> ''建议的是,您正在尝试获取所有没有空的 youtubesource 字段的行。

在这种情况下,您的代码可能如下所示:

$youtube = $database->getRows("SELECT youtubesource FROM news where youtubesource <> '' AND youtubesource is not null ORDER BY id DESC LIMIT 0,1");                    

您是否需要在要发送到函数的数组参数中使用 : ?

$youtube = $database->getRows("SELECT youtubesource FROM news where youtubesource = :youtubesource ORDER BY id DESC LIMIT 0,1",array('youtubesource'=>$youtubesource));

另外,这是对的吗:$youtubesource = "youtubesource <> ''";这不就是这样从新闻中选择YouTube来源,其中YouTube来源=youtubesource <> ''按ID定义限制排序或类似的东西