2个相同的SQL查询...一个起作用,一个得到错误消息“在null上调用成员函数prepare()". [英] 2 same SQL-Querys ... one works, one gets error message "Call to a member function prepare() on null"

查看:54
本文介绍了2个相同的SQL查询...一个起作用,一个得到错误消息“在null上调用成员函数prepare()".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个更大的数据库项目.现在,我变得好奇了,因为有相同的SQL查询,其中第一个得到的是错误,而第二个得到的却是正常的.我不知道.同时,我怀疑我的想法.

I work on an bigger database project. Now I become curious, because there are to same SQL-Query, where the first get's an error and the second, same, workes fine. I don't know. Meanwhile I doubt my mind.

这是简短的脚本部分:

$sql    = "SELECT * FROM `wt_name` WHERE `n_surname` LIKE 'A%' AND `n_file` = '4' AND `n_type` = 'NAME' GROUP BY `n_id` ORDER BY `n_surname`, `n_givn` ";

$statement      = $pdo->prepare($sql);
$erg            = $statement->execute();
$result         = $statement->fetchAll(PDO::FETCH_ASSOC);
$anzreihen      = $statement->rowCount();

$sqlANZ = "SELECT * FROM `wt_name` WHERE `n_surname` LIKE 'A%' AND `n_file` = '4' AND `n_type` = 'NAME' GROUP BY `n_id` ORDER BY `n_surname`, `n_givn` ";

$statementANZ   = $pdo->prepare($sqlANZ);
$erg2           = $statementANZ->execute();
$ergANZ         = $statementANZ->fetchAll(PDO::FETCH_ASSOC);
$anzahlGesamt   = $statementANZ->rowCount();

如果我不盲目,则$ sql和$ sqlANZ应该是同一查询. $ sqlANZ可以正常工作,而$ erganz可以获取应有的数据集.但是$ sql将不起作用,并给出错误消息包:

If I'm not blind, $sql and $sqlANZ should be the same query. $sqlANZ workes correct and $erganz get's the datasets it should. But $sql will not work and gives pack the error massage:

在null上调用成员函数prepare()

Call to a member function prepare() on null

同时我不知道该怎么办.有没有人可以检查脚本并帮助我,发现错误,我似乎看不到/找不到?

Meanwhile I don't anymore know, what to do. Is there anybody who can check the script and help me, find the error, I seemingly can't see/find?

(仅供参考:如果至少在实际版本中有效,则第一个查询将由LIMIT添加)

(only for information: the fist query will be added by a LIMIT, if it works in the actual version at least)

谢谢 巴吉拉

推荐答案

可以做到这一点?

$sql    = "
SELECT * 
  FROM `wt_name`
 WHERE  `n_surname`
  LIKE 'A%' 
   AND `n_file` = '4'
   AND `n_type` = 'NAME' 
 GROUP 
    BY `n_id`
 ORDER 
    BY `n_surname`
     , `n_givn`
";

然后pdo想要`"和"4"而不是4

then pdo wants "`" and '4' instead 4

这篇关于2个相同的SQL查询...一个起作用,一个得到错误消息“在null上调用成员函数prepare()".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆