使用php和mysql仅显示数据库中的内容一次
I'm trying to show images with a specific subject on the screen. I have 5 images with the same subject and it only shows one image. If I change the subject of the image in the database, the next image with the subject I try to call appears.
function selectSubject($subject){
$showSubject = mysql_query("SELECT name FROM images WHERE subject = '$subject'");
while($showSubject = mysql_fetch_array($showSubject)){
$source_subject_trees = $showSubject['name'];
echo "<img class=\"subject_images\" src='img/$source_subject_bomen'></>";
}
};
我正在尝试在屏幕上显示具有特定主题的图像。 我有5个具有相同主题的图像,它只显示一个图像。 如果我更改数据库中图像的主题,则会显示我尝试调用的主题的下一个图像。 p>
function selectSubject($ subject){
$ showSubject = mysql_query(“SELECT name FROM images WHERE subject ='$ subject'”);
while($ showSubject = mysql_fetch_array($ showSubject)){
$ source_subject_trees = $ showSubject ['name'];
echo“&lt; img class = \“subject_images \”src ='img / $ source_subject_bomen'&gt;&lt; /&gt;“;
}
};
code> pre>
div>
mysql_query()
returns a result handle. You then stomp all over that handle by re-assigning to it within your while() loop:
$showSubject = mysql_query(...);
while($showSubject = msyql_fetch_array($showSubject)) {
^^^^^^^^^^^^---here
mysql_fetch_array()
returns an array of one row's data. Since you're assigning to the SAME variable as you stored the actual query result handle, you destroy the result handle... and end up being unable to fetch any more data, because the handle's gone.
- In
mysql_query
you can useGROUP BY subject
to get only distinct result - In
mysql_query
you can useDISTINCT(name)
to get distinct result on selection time
Any of aboue mention option to get as per your valid result.