![](/img/trans.png)
[英]Any particular reason why this piece of code doesn't work? I can't seem the figure out the issue
[英]Why doesn't this piece of php code work?
大家好,我有一个mysql数据库,其中存储着每张图片的ID,以及图片的网址和缩略图的网址。 缩略图URL类似于thumbs / pic.jpg,我正在创建的页面在创建的同一位置具有thumbs文件夹。 但是,当我运行这段代码时,什么都没有显示:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
mysql_connect("localhost", "***", "***") or die(mysql_error());
mysql_select_db("pics") or die(mysql_error());
$results = mysql_query("select id from classpics") or die(mysql_error());
$data=mysql_fetch_array($results);
foreach($data as $uno) {
echo '<td valign="middle" align="center"><a class="neutral" href="../images.php?id=' . $uno['id'] . '"><img src="'. $uno['thumbs'].'" border="0" /></a></td>';
}
?>
至少有两个错误。
您需要调用mysql_fetch_array:
while($data = mysql_fetch_array($results)) {
...
}
而且您没有选择大拇指:
"select id, thumbs from classpics"
用户while循环以获取数据
并且还调用mysql_fetch_assoc
因为您使用列名作为数组索引
while($uno=mysql_fetch_assoc($results)) {
echo '<td valign="middle" align="center"><a class="neutral" href="../images.php?id=' . $uno['id'] . '"><img src="'. $uno['thumbs'].'" border="0" /></a></td>';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.