2 same SQL-Querys … one works, one gets error message “Call to a member function prepare() on null” [duplicate]

被刻印的时光 ゝ 提交于 2020-03-05 06:04:13

问题


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.

Here are the short script part:

$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();

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:

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?

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

Thanks bagira


回答1:


works this?

$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`
";

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



来源:https://stackoverflow.com/questions/60021528/2-same-sql-querys-one-works-one-gets-error-message-call-to-a-member-functi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!