我想通过设置需要忽略另一个变量中的where
子句的sql来忽略where
子句。
但是当我尝试运行时,没有显示任何内容。
当我删除sql变量"$test"
并在$requete
内设置连接时。然后它会工作,但不会忽略where子句:
<?php
// List of events
$json = array();
// Query that retrieves events
$requete = " SELECT distinct `evenementontvanger`.`idEvent`,`evenementontvanger`.`idProject`,`evenementontvanger`.`idKlant`,
`evenement`.`id`,`evenement`.`title`,`evenement`.`start`,`evenement`.`startdate`,`evenement`.`starttime`,
`evenement`.`end`,`evenement`.`enddate`,`evenement`.`endtime`,`evenement`.`description`,`evenement`.`color`
FROM `evenement` LEFT JOIN `evenementontvanger` ON `evenementontvanger`.`idEvent` = `evenement`.`id` WHERE `idEvent`=`evenement`.`id`";
$test ="LEFT JOIN `Werknemer` ON `Werknemer`.`idWerknemer`=`evenementontvanger`.`idWerknemer`";
// connection to the database
try {
$bdd = new PDO('mysql:host=localhost;dbname=blackboks-calendar', 'calendar-boks', '19xantia');
} catch(Exception $e) {
exit('Unable to connect to database.');
}
// Execute the query
$resultat = $bdd->query($requete,$test) or die(print_r($bdd->errorInfo()));
// sending the encoded result to success page
echo json_encode($resultat->fetchAll(PDO::FETCH_ASSOC));
?>
答案 0 :(得分:2)
<?php
$foo = true;
// Query that retrieves events
if ( $foo ) {
$auxJoin ="
LEFT JOIN
`Werknemer`
ON
`Werknemer`.`idWerknemer`=`evenementontvanger`.`idWerknemer`
";
}
else {
$auxJoin = '';
}
$requete = "
SELECT
distinct `evenementontvanger`.`idEvent`,`evenementontvanger`.`idProject`,`evenementontvanger`.`idKlant`,
`evenement`.`id`,`evenement`.`title`,`evenement`.`start`,`evenement`.`startdate`,`evenement`.`starttime`,
`evenement`.`end`,`evenement`.`enddate`,`evenement`.`endtime`,`evenement`.`description`,`evenement`.`color`
FROM
`evenement`
LEFT JOIN
`evenementontvanger`
ON
`evenementontvanger`.`idEvent` = `evenement`.`id`
$auxJoin
WHERE
`idEvent`=`evenement`.`id`
";
// connection to the database
try {
$bdd = new PDO('mysql:host=localhost;dbname=blackboks-calendar', 'calendar-boks', '19xantia');
$resultat = $bdd->query($requete,$test) or die(print_r($bdd->errorInfo()));
$data = $resultat->fetchAll(PDO::FETCH_ASSOC)
}
catch(Exception $e) {
$data = array('error'=>'Unable to connect to database.');
}
// sending the encoded result to success page
echo json_encode($data);
答案 1 :(得分:1)
$test = "LEFT JOIN Werknemer W ON W.idWerknemer = et.idWerknemer";
$sql = "SELECT ... LEFT JOIN ... FROM ON idEvent = e.id $test WHERE ...";
现在您可以动态添加或删除该部分