我在这段代码中遇到问题,我从两个表中获取信息,并且“where news_id=$dz”不起作用。 Here is the page其中包括此代码。
这是我的代码:
<?php
mysql_query("SET NAMES UTF8");
$result = mysql_query("SELECT dz,title FROM dzeglebi where raioni='ყვარლის მუნიციპალიტეტი' && mxare='kaxeti' ORDER BY title ASC", $db);
$myrow = mysql_fetch_array ($result);
printf(
"<h2><li>
<strong><a href='../../dzeglebi.php?id=%s'>%s</a>
</strong></li></h2>",$myrow["dz"],$myrow["title"]);
function FetchImage($id)
{
$images=array();
$x=0;
$d=mysql_query("select * from `images` where news_id=$dz");
while($data=mysql_fetch_array($d))
{
$images["big"][$x]=$data["image"];
$images["small"][$x]=$data["small"];
$x++;
}
return $images;
}
function CountImages($id)
{
$d=mysql_query("select * from `images` where news_id=$dz");
return mysql_num_rows($d);
}
$imgs=FetchImage($id);
for($i=0;$i<CountImages($id);$i++)
{
echo'
<img src="../'.$imgs["big"][$i].'" >';
}
?>
最佳答案
function CountImages($id)
{
$d=mysql_query("select * from images where news_id=$dz");
- 这里你应该使用$id
下一个问题: - 不要使用for($i=0;$i<CountImages($id);$i++)
将其更改为
$len = CountImages($id);
for($i=0;$i<$len;$i++)
因为你不需要为每个 FOR 迭代执行 SQL 查询,甚至删除它 - 因为你有 $images 数组,所以迭代它
下一个问题:
$d=mysql_query("select * from `images` where news_id=$id");
return mysql_num_rows($d);
请勿使用mysql_num_rows
,使用 SQL count(*)
- 因为它只会从 mysql 发送 1 个数字到 php,而不是整个结果集
关于php - MySQL WHERE 查询不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19305118/