查询显示条目数的相同结果
我使用下面的代码,对于每个条目,它都会重复最近的条目,而不是显示所有条目。
代码:
$sql="SELECT theday.day_id, actornames.actorname,
theday.personwhois, actornames.whyactor,
theday.daydate FROM theday LEFT JOIN actornames
ON theday.personwhois = actornames.actor_id WHERE
actornames.group_id = '$gi' ORDER BY theday.id DESC";
$result=mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
$result = mysql_query($sql);
if ($result == "")
{
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if($rows == 0)
{
print("");
}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
{
// All other fields I am using the $variable method below to show them
$day = mysql_result($result,$i,"day_id");
示例:
如果我有十个唯一行,它将使用最新条目重复十行:
Angelina Jolie, Wanted, Original Sin, Taking lives
Angelina Jolie, Wanted, Original Sin, Taking lives
Angelina Jolie, Wanted, Original Sin, Taking lives
Angelina Jolie, Wanted, Original Sin, Taking lives
Angelina Jolie, Wanted, Original Sin, Taking lives
Angelina Jolie, Wanted, Original Sin, Taking lives
Angelina Jolie, Wanted, Original Sin, Taking lives
Angelina Jolie, Wanted, Original Sin, Taking lives
Angelina Jolie, Wanted, Original Sin, Taking lives
Angelina Jolie, Wanted, Original Sin, Taking lives
I am using the code below and for each entry it is repeating the most recent entry instead of showing all entries.
Code:
$sql="SELECT theday.day_id, actornames.actorname,
theday.personwhois, actornames.whyactor,
theday.daydate FROM theday LEFT JOIN actornames
ON theday.personwhois = actornames.actor_id WHERE
actornames.group_id = '$gi' ORDER BY theday.id DESC";
$result=mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
$result = mysql_query($sql);
if ($result == "")
{
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if($rows == 0)
{
print("");
}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
{
// All other fields I am using the $variable method below to show them
$day = mysql_result($result,$i,"day_id");
Example:
If i have ten unique rows it repeats ten rows with the most recent entries:
Angelina Jolie, Wanted, Original Sin, Taking lives
Angelina Jolie, Wanted, Original Sin, Taking lives
Angelina Jolie, Wanted, Original Sin, Taking lives
Angelina Jolie, Wanted, Original Sin, Taking lives
Angelina Jolie, Wanted, Original Sin, Taking lives
Angelina Jolie, Wanted, Original Sin, Taking lives
Angelina Jolie, Wanted, Original Sin, Taking lives
Angelina Jolie, Wanted, Original Sin, Taking lives
Angelina Jolie, Wanted, Original Sin, Taking lives
Angelina Jolie, Wanted, Original Sin, Taking lives
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否输出行变量的结果?
首先尝试清理代码,下一个示例代码应该执行查询的基本部分。
Are you outputing the result from the row variable?
Try cleaning up the code first, the next sample code should do the basic part of the querying.
您的代码存在多个问题。
a) 不要运行查询三次,并且不要存储结果资源的两个副本(
$query
和$results
是重复的)。这是对资源的巨大浪费。b) 您正在循环
$query
(在 while 循环中),同时尝试从$result
中提取数据。$result
的内部指针不会更新,因此您永远不会从第一行前进。同样,只需运行一次查询即可解决此问题。c) mysql_result 的第二个参数是行的数字偏移量。您使用的是
$i
,它未定义,因此其默认值为 0。将其更改为$row
并不能解决您的问题,因为$row
是一个数组。您需要使用计数器。或者,更好的是,由于您已经在获取数组,所以不要使用 mysql_result 句点,因为它效率低下且冗余。There are multiple problems with your code.
a) Don't run the query three times, and don't store two copies of the results resource (
$query
and$results
are duplicates). This is a giant waste of resources.b) You're looping through
$query
(in your while loop), while trying to pull data from$result
. The internal pointer of$result
is not being updated and so you will never advance from the first row. Again, only running the query once would solve this.c) The 2nd argument of
mysql_result
is the numerical offset of the row. You're using$i
, which isn't defined so its default value is 0. Changing it to$row
didn't fix your problem because$row
is an array. You would need to use a counter. Or, even better, since you're already fetching the array, don't usemysql_result
period since it's inefficient and redundant.我只需要使用 $row['row_name'];而不是我拥有的。
I just needed to use $row['row_name']; instead of what i had.