问题
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