1

我在这里的问题(可能)是 $db->fetch_array 不会显示数据库中的所有结果。我不知道会发生什么,但我只得到了 3 个结果中的 1 个,即使我稍微更改了查询,我也尝试了很多东西。你有什么想法为什么我不能在这里得到所有的结果吗?

它适用于 vBulletin 3.8 顺便说一句。

谢谢人们。

if ($_REQUEST['do'] == 'showthis') {


    $rel = $db->query_first("
        SELECT *
        FROM " . TABLE_PREFIX . "anexampletable
        WHERE fromuserid OR touserid = " . $vbulletin->userinfo['userid'] . "
        AND confirmstatus =1
    ");


if ($rel)  {

$queryrel = $db->query_read("
                SELECT *
        FROM " . TABLE_PREFIX . "anexampletable
        WHERE fromuserid OR touserid = " . $vbulletin->userinfo['userid'] . "
        AND confirmstatus =1
        ");            

while ($queryre = $db->fetch_array($queryrel)) { 

if ($queryre['reltype'] == '1') {

               $ty = " is something else ";

} else if ($queryre['reltype'] == '2') {

               $ty = " is something ";

} else if ($queryre['reltype'] == '3') {

               $ty = " is something else ";

} else if ($queryre['reltype'] == '4') {

               $ty = " is something ";

} else if ($queryre['reltype'] == '5') {

               $ty = " is something ";

} else if ($queryre['reltype'] == '6') {

               $ty = " is something ";

} else if ($queryre['reltype'] == '7') {

               $ty = " is something ";

} else if ($queryre['reltype'] == '8') {

               $ty = " is something ";

} else if ($queryre['reltype'] == '9') {

               $ty = " is something ";

} else if ($queryre['reltype'] == '10') {

               $ty = " is something ";

} else if ($queryre['reltype'] == '11') {

               $ty = " is something else ";

} else if ($queryre['reltype'] == '12') {

               $ty = " is something else ";

} else if ($queryre['reltype'] == '13') {

               $ty = " is something ";

} else if ($queryre['reltype'] == '14') {

               $ty = " is something ";
} else {

$ty = " is default ";

}

$sender = $queryre['fromusername'];
$receiver = $queryre['tousername'];
$showit = $sender . $ty . $receiver;

}

eval('print_output("' . fetch_template('relationships') . '");');

}

}   
4

1 回答 1

0

您的查询是说:

WHERE `fromuserid` has any value 

OR (touserid = $vbulletin->userinfo['userid']).

如果您想要与用户标识匹配fromuseridtouserid与用户标识匹配的行,请尝试以下操作:

$queryrel = $db->query_read("
        SELECT * FROM " . TABLE_PREFIX . "anexampletable

        WHERE (fromuserid = " . $vbulletin->userinfo['userid'] . ") 
        OR (touserid = " . $vbulletin->userinfo['userid'] . ")

        AND confirmstatus =1
        ");

更新:
在不知道您正在使用的数据的情况下很难确定问题。我创建了一个测试,输出您的代码正在使用的数据,您将能够看到查询的各个部分正在返回什么,然后可以确定问题所在。

通过将此代码放在示例中的代码之前临时修改您的文件(您需要使用正确的表名)。然后编辑您的问题并将输出粘贴到底部。

echo "vBulletin User ID = " . $vbulletin->userinfo['userid'];

$test_query1 = $db->query_read("
  SELECT * FROM " . TABLE_PREFIX . "anexampletable
  WHERE (fromuserid = " . $vbulletin->userinfo['userid'] . ")
");  

$t1_count = 0;
echo "Test From User ID Results<br />";
while ($test1_output = $db->fetch_array($test_query1)) {
  $t1_count++;
  echo "Test From User Result " . $t1_count . "<br />";
  echo "From User ID = " . $test1_output['fromuserid'] . "<br />";
  echo "To User ID = " . $test1_output['touserid'] . "<br />";
  echo "Confirm Status = " . $test1_output['confirmstatus'] . "<br />";
  echo "Relationship Status = " . $test1_output['reltype'] . "<br />";
}


$test_query2 = $db->query_read("
  SELECT * FROM " . TABLE_PREFIX . "anexampletable
  WHERE (touserid = " . $vbulletin->userinfo['userid'] . ")
");  

$t2_count = 0;
echo "<br /><br />Test To User ID Results<br />";
while ($test2_output = $db->fetch_array($test_query2)) {
  $t2_count++;
  echo "Test To User Result " . $t2_count . "<br />";
  echo "From User ID = " . $test2_output['fromuserid'] . "<br />";
  echo "To User ID = " . $test2_output['touserid'] . "<br />";
  echo "Confirm Status = " . $test2_output['confirmstatus'] . "<br />";
  echo "Relationship Status = " . $test2_output['reltype'] . "<br />";
}

exit();

最终代码?
看来有两个问题:
1)查询需要修改:
原始:

fromuserid OR touserid = " . $vbulletin->userinfo['userid'] . "

更新:

(fromuserid = " . $vbulletin->userinfo['userid'] . " 
OR 
touserid = " . $vbulletin->userinfo['userid'] . ")

更新于 2012 年 7 月
5 日 2)您不能在 vb3 模板中循环遍历数组,因此我们将连接字符串。
在模板中使用的 $showit 变量是一个字符串。每次连续通过 while 循环都会覆盖它,因此只有最后一个结果被发送到模板。而不是使用$showit = xxx;,使用$showit .= xxx;with .=

我已经更新了下面代码的最后 15 行左右。


您可以查看论坛页面的生成方式以了解更多信息。
打开upload\forumdisplay.php 文件。while创建线程列表的循环从这里开始:
upload\forumdisplay.php(962)
while ($thread = $db->fetch_array($threads))

每个线程的输出使用“threadbit”模板生成并添加到$threadbit此处的字符串中:
upload\forumdisplay.php(1000)
eval('$threadbit .= "' . fetch_template('threadbit') . '";');

最后输出“FORUMDISPLAY”模板:
upload\forumdisplay.php(1056)
eval('print_output("' . fetch_template('FORUMDISPLAY') . '");');

如果您查看 FORUMDISPLAY 模板,您会发现$threadbit字符串从开头使用了大约 1/5。


试试下面的代码,看看它是如何工作的,我else if用一个语句替换了这一系列switch()语句。它更有效率。

if ($_REQUEST['do'] == 'showthis') {

  // Make standalone query, easy to output query string and run it directly for testing
  $rel_sql = "SELECT * FROM " . TABLE_PREFIX . "anexampletable
    WHERE (fromuserid = " . $vbulletin->userinfo['userid'] . "
    OR touserid = " . $vbulletin->userinfo['userid'] . ")
    AND confirmstatus =1";

  $queryrel = $db->query_read($rel_sql);

  if ($db->num_rows($queryrel))
  {
    while ($queryre = $db->fetch_array($queryrel))
    {
      switch ($queryre['reltype'])
      {
        case 1:
          $ty = " do something 1 ";
          break;
        case 2:
          $ty = " do something 2 ";
          break;
        case 3:
          $ty = " do something 3 ";
          break;

        // Add as many cases as needed
        .......
        case xxx:
          $ty = " do something xxx ";
          break;
        .......

        default:
          $ty = " is default ";
      }

      $sender = $queryre['fromusername'];
      $receiver = $queryre['tousername'];

      // UPDATED FROM HERE DOWN on 07/05/2012
      // Add to $showit with ".=" rather than overwriting it with "=".

      // Method One
      // If the output is simple, try this.
      // I added a line break after each entry.
      $showit .= $sender . $ty . $receiver . "<br />";

      OR

      // Method Two
      // If the output is complex.
      // Create a separate template and store the output in $showit
      // Remember to add the new template to the $actiontemplates array.
      eval('$showit .= "' . fetch_template('showit') . '";');
    }

    eval('print_output("' . fetch_template('relationships') . '");');
  }
}
于 2012-06-15T00:42:15.447 回答