du21064 2015-12-05 08:14
浏览 71
已采纳

将变量放入PDO语句中

is it possible to create a pdo query with a variable? Example:

$q = "SELECT COUNT (*) c FROM blogpages WHERE keywords LIKE '%test%' ";

then

$query = $db->query("$q");

$result = $query->fetch(PDO::FETCH_ASSOC);

when i do this i get an error

"Call to a member function fetch() on a non-object in C....."

i want to know is there a way to place the query in there as a variable because the query changes depending on how many OR statements are in the query

  • 写回答

1条回答 默认 最新

  • duanaigua4033 2015-12-05 08:25
    关注

    query()

    PDO::query — Executes an SQL statement, returning a result set as a PDOStatement object

    And problem in your query with space between count and (*)

    SELECT COUNT (*)..
                ^^
    

    SO no need to fetch data just use

    $q = "SELECT COUNT(*) c FROM blogpages WHERE keywords LIKE '%test%' ";
    foreach ($db->query($q) as $row) {
            print $row['c'] . "\t";
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?