Bonjour à tous et joyeuses fêtes.
Dans une application de généalogie, je cherche à faire une sélection de parents possibles.
J'ai une fonction MySQL avec le code suivant. Dans cette fonction la clause WHERE parait correcte mais je n'arrive pas à écrire le tableau des paramètres pour la fonction execute() (cf. ligne 32):
Code PHP : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function searchParents(int $idAbo, array $clauses=[]): array|false {
	$db = dbConnect();
	var_dump($clauses);
 
	$where	= [];
	$where[]= "id_abo=:idAbo";
	$params	= [':idAbo'=>$idAbo, ];
 
	foreach($clauses as $key=>$clause) {
 
		if( in_array($key, ['gender', 'lastname', 'firstname', ]) ){
			$where[]		= "{$key}=:{$key}";
			$params[":$key"]= "{$key}";
		}
 
	}
 
	if( !empty('birthY1') && !empty('birthY2') ){
		$where[] = "( birthyear BETWEEN :birthY1 AND :birthY2 ) OR birthyear IS NULL";
		$params[":birthY1"] = "birthY1";
		$params[":birthY2"] = "birthY2";
	}
 
	if( !empty('deathY1') && !empty('deathY2') ){
		$where[] = "( deathyear BETWEEN :deathY1 AND :deathY2 ) OR deathyear IS NULL";
		$params[":deathY1"] = "deathY1";
		$params[":deathY2"] = "deathY2";
	}
 
	$where = implode("\nAND ", $where);
	var_dump($where);
	var_dump($params);
 
 
	$sql = <<<SQL
	SELECT
		id, CONCAT( COALESCE(UPPER(lastname), ''), ' ', COALESCE(firstname, ''), ' ', COALESCE(middlename, '') ) AS fullname,
		CONCAT( COALESCE(birthday, '--'), '/', COALESCE(birthmonth, '--'), '/', COALESCE(birthyear, '----') ) AS birthdate,
		' - ', CONCAT( COALESCE(deathday, '--'), '/', COALESCE(deathmonth, '--'), '/', COALESCE(deathyear, '----') ) AS deathdate
		FROM dat_persons
		WHERE {$where}
		ORDER BY lastname, firstname, birthyear, birthmonth, birthday
	SQL;
	$stmt = $db->prepare($sql);
	$stmt->execute($params);
	var_dump($stmt->fetchAll() );
	return $stmt->fetchAll();
}
Je pense que c'est le tableau d'entrée qui n'est pas bon (mal construit avec valeurs au lieu de variables).
Je vous donne les valeurs retournées par les var_dump():
C:\wamp64\www\proginet\appGenealium\model\model.php:3:
array (size=4)
  'gender' => string 'M' (length=1)
  'lastname' => string 'Paris' (length=5)
  'birthY1' => string '1831' (length=4)
  'birthY2' => string '1907' (length=4)

C:\wamp64\www\proginet\appGenealium\model\model.php:31:string 'id_abo=:idAbo
AND gender=:gender
AND lastname=:lastname
AND ( birthyear BETWEEN :birthY1 AND :birthY2 ) OR birthyear IS NULL
AND ( deathyear BETWEEN :deathY1 AND :deathY2 ) OR deathyear IS NULL' (length=193)

C:\wamp64\www\proginet\appGenealium\model\model.php:32:
array (size=7)
  ':idAbo' => int 2
  ':gender' => string 'gender' (length=6)
  ':lastname' => string 'lastname' (length=8)
  ':birthY1' => string 'birthY1' (length=7)
  ':birthY2' => string 'birthY2' (length=7)
  ':deathY1' => string 'deathY1' (length=7)
  ':deathY2' => string 'deathY2' (length=7)