查询显示条目数的相同结果

发布于 2024-11-04 07:31:03 字数 1410 浏览 0 评论 0原文

我使用下面的代码,对于每个条目,它都会重复最近的条目,而不是显示所有条目。

代码:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

骷髅 2024-11-11 07:31:03

您是否输出行变量的结果?

首先尝试清理代码,下一个示例代码应该执行查询的基本部分。

$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);

while($row = mysql_fetch_array($result))
{
   print_r($row);
   // additional formatting
}

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.

$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);

while($row = mysql_fetch_array($result))
{
   print_r($row);
   // additional formatting
}
自演自醉 2024-11-11 07:31:03

您的代码存在多个问题。

a) 不要运行查询三次,并且不要存储结果资源的两个副本($query$results 是重复的)。这是对资源的巨大浪费。

b) 您正在循环 $query(在 while 循环中),同时尝试从 $result 中提取数据。 $result 的内部指针不会更新,因此您永远不会从第一行前进。同样,只需运行一次查询即可解决此问题。

c) mysql_result 的第二个参数是行的数字偏移量。您使用的是 $i,它未定义,因此其默认值为 0。将其更改为 $row 并不能解决您的问题,因为 $row 是一个数组。您需要使用计数器。或者,更好的是,由于您已经在获取数组,所以不要使用 mysql_result 句点,因为它效率低下且冗余。

$sql = ...
$result = mysql_query($sql) or die ("Error: ".mysql_error());
$rows = mysql_num_rows($result);

if($rows > 0) 
{
    while($row = mysql_fetch_assoc($result))
    {
        $day = $row['day_id'];
        // ...
    }
}

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 use mysql_result period since it's inefficient and redundant.

$sql = ...
$result = mysql_query($sql) or die ("Error: ".mysql_error());
$rows = mysql_num_rows($result);

if($rows > 0) 
{
    while($row = mysql_fetch_assoc($result))
    {
        $day = $row['day_id'];
        // ...
    }
}
霞映澄塘 2024-11-11 07:31:03

我只需要使用 $row['row_name'];而不是我拥有的。

I just needed to use $row['row_name']; instead of what i had.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文