我有一个搜索结果页面,其中显示来自MySQL数据库(table1)的项目。我用来显示结果的代码是:
if (!empty($data)) {
foreach ($data as $item) {
echo '<div class="item">';
if (strlen($item['item_desc']) > 10) {
if (strlen($item['item_link']) > 10) {
echo '<a href="/item.php?id='.$item['item_id'].'">';
} else {
echo 'No Results Found';
}
}
}
}每个搜索结果的图像存储在单独的表(table2)中。我正在尝试使用下面的代码来计算每个结果在table2中的图像数量,并在搜索结果页面上显示每个结果的数量,但它返回了一个0值?
$result=mysql_query("SELECT count(*) as total from table2 where id = '" .$item['item_id']. "'");
$query=mysql_fetch_assoc($result);
echo $query['total'];发布于 2015-09-13 01:52:26
我遗漏了一些关于您的数据库结构的数据,但是我可以想象您将图像id存储为一个整数,并且您正在请求id为字符串的计数。
因此,如果我是真的,这可能会有所帮助。用item_id去掉php变量两边的引号
"SELECT count(*) as total from table2 where id = " .$item['item_id'];
发布于 2015-09-13 03:32:18
你可以试试这个..
$str=$item['item_id'];
mysql_query("SELECT count(*) as total from table2 where id = '$str'");发布于 2015-09-13 18:16:58
有一个mysql函数可以计算mysql查询输出的行数,它是mysql_num_rows().。根据我对你的数据库的理解,你的代码可能是:
$item_id = $item['item_id'];
$result = mysql_query("SELECT * FROM table2 where id = '$item_id'");
$count = mysql_num_rows($result);
echo $count;这将输出有多少条记录与您的查询匹配。
https://stackoverflow.com/questions/32541816
复制相似问题