我有这个代码用于从数据库中获取数据:
function show_volanti($data){
$con = $data; // PASSO CONNESSIONE
$id = 1; // 1 VOLANTE
$visibile = 1; // VARIABILE DI VISIBILITA'
$rows1 = array(); // PREPARO ARRAY 1 PER ID ARTICOLI VOLANTE
$rows2 = array(); // PREPARO ARRAY 2 PER LE FOTO VOLANTI
$id_articoli = ''; // RIFERIMENTO ARTICOLI PER SECONDA QUERY GALLERIA
$g = ''; // RIFERIMENTO ASSOCIAZIONE GALLERY VIEWER
//$rif_id = ''; // RIF_ID SE OK DA CANCELLARE
$query1 = "SELECT articoli.id AS id_articoli,
articoli.titolo,
articoli.descrizione
FROM articoli
WHERE articoli.genere1 = ?
AND articoli.visibile = ?";
$query2 = "SELECT galleria.id AS id_galleria,
galleria.foto,
galleria.rif_id
FROM galleria
WHERE galleria.rif_id = ?";
$stmt = mysqli_stmt_init($con); // INIZIALIZZO LA CONNESSIONE
mysqli_stmt_prepare($stmt,$query1);
mysqli_stmt_bind_param($stmt,'ii',$id,$visibile); // LEGO I PARAMETRI
mysqli_stmt_execute($stmt); // ESEGUO LA QUERY
mysqli_stmt_bind_result($stmt,
$rows1['id_articoli'],
$rows1['titolo'],
$rows1['descrizione']); // CREO RIFERIMENTO PER GALLERIA NEL VIEWER
$html = "";
$html .= "<div class='container'>";
while (mysqli_stmt_fetch($stmt)){
$id_articoli = $rows1['id_articoli'];
$html .= " <div class='row'>";
$html .= " <div class='col-sm-12'>";
$html .= " <div class='panel panel-default'>";
$html .= " <div class='panel-body'>";
$html .= " <div class='col'>";
$html .= " <div class='panel panel-default'>";
$html .= " <div class='panel-heading'><b>$rows1[titolo]</b></div>";
$html .= " <div class='panel-body'>";
$html .= " <div class='row'>";
$html .= " <div class='class_p'>$rows1[descrizione]</div>";
$html .= " <div> <!-- end first row -->";
$html .= " <div class='class_container clearfix'>";
mysqli_stmt_prepare($stmt,$query2);
mysqli_stmt_bind_param($stmt,'i',$id_articoli); // LEGO I PARAMETRI
mysqli_stmt_execute($stmt); // ESEGUO LA QUERY
mysqli_stmt_bind_result($stmt,
$rows2['id_galleria'],
$rows2['foto'],
$rows2['rif_id']);
while(mysqli_stmt_fetch($stmt)){
$g = '';
$g .= "g";
$g .= $rows2['rif_id'];
$html .= "<div class='thumbnail col-sm-2'>";
$html .= "<div class='class_img'>";
$html .= "<a href='$rows2[foto]' data-toggle='lightbox' data-gallery='$g' >";
$html .= "<img src='$rows2[foto]' class='img-fluid'>";
$html .= "</a>";
$html .= "</div> <!-- end class_img -->";
$html .= "</div> <!-- end thumbnail col-sm-2- -->";
}
$html .= "</div> <!-- end class_container -->";
$html .= "</div> <!-- end panel body -->";
$html .= "</div> <!-- end panel panel-default -->";
$html .= "</div> <!-- end col -->";
$html .= "</div> <!-- end panel-body -->";
$html .= "</div> <!-- end panel panel-default -->";
$html .= "</div> <!-- end col-sm-12 -->";
$html .= "</div> <!-- end row -->";
}
mysqli_stmt_close($stmt); // CHIUDO LO STATEMENT
mysqli_close($con); // CHIUDO CONNESSIONE
return $html;
}
代码部分工作,因为它显示结果,但它只显示一个结果,在数据库中我有更多的结果显示所以它不能正常工作..你可以帮我找到错误吗?
答案 0 :(得分:0)
您似乎正在为外部和内部循环重复使用相同的变量。例如,您为这两者使用$ stmt。我不确定这是不是为什么你只得到一个结果(因为它可能取决于你从每个结果中得到的结果数量),但我会调查一下。
作为调试提示,我将删除与外部循环相关的所有代码,并首先正确打印外部循环的结果。一旦你有了工作,开始添加内部循环代码并确保它工作。不要重复使用相同的变量。