MySQL / PHP foreach仍然只在数据库中显示 [英] MySQL/PHP foreach still only displaying first in database
本文介绍了MySQL / PHP foreach仍然只在数据库中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从数据库中查询结果,其中应查询多个结果。但是,当我尝试显示查询的结果,只有一个结果显示,所以我试图使用foreach函数,但它仍然不工作。我被打败了,不知道我做错了什么。
I am querying results from a database, where more than one result should be queried. However, when I tried displaying the result of the query, only one result showed, so I tried to use a foreach function, but it's still not working. I'm beat, no idea what I'm doing wrong. Anyone have a good idea of what's going wrong?
以下是MySQL查询代码:
Here's the MySQL query code:
<?php
//Database Information
$dbhost = "";
$dbname = "";
$dbuser = "";
$dbpass = "";
//Connect to database
mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$filename = $_GET['filename'];
$new_captions = mysql_query("SELECT * from captions where image = 'http://math.stanford.edu/inc/img/PalmDrive.png' ORDER BY idnum DESC LIMIT 5");
while($rows = mysql_fetch_array($new_captions)){
$caption = $rows;
}
?>
这里是foreach:
And here's the foreach:
<?php foreach($caption as $rows) {?>
<div id="set_caption" style="width:<?php echo $caption['width'];?>px; height:<?php echo $caption['height'];?>px; left:<?php echo $caption['posleft'];?>px; top:<?php echo $caption['postop'];?>px;"><?php echo $caption['text'];?></div>
<?php } ?>
推荐答案
您有以下错误。
- $ caption未声明。
- 使用array_push或$ caption [] = $ rows ;制作字幕数组。
-
在foreach中使用$ row变量。
- $caption is not declare before.
- use array_push or $caption[] = $rows; to make caption array.
Use $row variable in the foreach.
//Database Information
$dbhost = "";
$dbname = "";
$dbuser = "";
$dbpass = "";
//Connect to database
mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$filename = $_GET['filename'];
$new_captions = mysql_query("SELECT * from captions where image = 'http://math.stanford.edu/inc/img/PalmDrive.png' ORDER BY idnum DESC LIMIT 5");
$caption = array();
while($rows = mysql_fetch_array($new_captions)){
$caption[] = $rows;
}
foreach($caption as $row) {
<div id="set_caption"
style="width:<?php echo $row['width'];?>px;
height:<?php echo $row['height'];?>px;
left:<?php echo $row['posleft'];?>px;
top:<?php echo $row['postop'];?>px;">
<?php echo $row['text'];?>
</div>
}
这篇关于MySQL / PHP foreach仍然只在数据库中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文