我是 MySQL 的新手。看起来我需要在 MySQL 请求中执行某种循环,但我认为使用一些“INNER JOIN”内容可能会更有效。
这是我的PHP代码:
$query = 'SELECT id FROM membres WHERE pseudo = :pseudo LIMIT 1';
$req = $dtb -> prepare($query);
$req -> execute(array(
'pseudo' => $_COOKIE['pseudo']
));
while($donnes = $req -> fetch()){
$id_pseudo = $donnes['id'];
}
$req -> closeCursor();
$query = 'SELECT id_chanson FROM samples WHERE id_membre = :id_pseudo';
$req = $dtb -> prepare($query);
$req -> execute(array(
'id_pseudo' => $id_pseudo
));
$id_chansons = array();
while($donnes = $req -> fetch()){
$id_chansons[] = $donnes['id_chanson'];
}
$req->closeCursor();
$nSongs= count($id_chansons);
$query = 'SELECT nom, pathName, date FROM chansons WHERE id = :id_chanson';
$req = $dtb -> prepare($query);
for($i=0;$i<$nSongs;$i++){
$req -> execute(array(
'id_chanson' => $id_chansons[$i]
));
while($donnes = $req -> fetch()){
$nomChanson[$i] = $donnes['nom'];
$pathName[$i] = $donnes['pathName'];
$date[$i] = $donnes['date'];
}
}
编辑:我的表名是“chansons”(法语歌曲)“membres”(用户)和“samples”
:)