-2

我对 PHP 很陌生,我有一个测试项目

我想让代码更高效,因为它需要一些时间来生成。非常感谢,纳特

它使用的代码如下:

    $result1 = mysql_fetch_assoc(mysql_query("SELECT COUNT(*) AS `Rows`, `1` FROM `things` GROUP BY  `1` ORDER BY RAND() LIMIT 0 , 1"));
    $result2 = mysql_fetch_assoc(mysql_query("SELECT COUNT(*) AS `Rows`, `2` FROM `things` GROUP BY  `2` ORDER BY RAND() LIMIT 0 , 1"));
    $result3 = mysql_fetch_assoc(mysql_query("SELECT COUNT(*) AS `Rows`, `3` FROM `things` GROUP BY  `3` ORDER BY RAND() LIMIT 0 , 1"));
    $result4 = mysql_fetch_assoc(mysql_query("SELECT COUNT(*) AS `Rows`, `4` FROM `things` GROUP BY  `4` ORDER BY RAND() LIMIT 0 , 1"));

    echo $result1["1"];
    echo " ";
    echo $result2["2"];
    echo " ";
    echo $result3["3"];
    echo " ";
    echo $result4["4"];

    mysql_close($con);      
    ?>
4

1 回答 1

0

我假设善意并使用您想要使用的方法清理了以最佳方式呈现的代码。

<?

$count = 1;
$results = array();
for ($count; $count <= 4; $count++) {
  $query = sprintf("SELECT COUNT(*) AS `Rows`, `%d` FROM `things` GROUP BY  `1` ORDER BY RAND() LIMIT 1", $count)
  $results[] = mysql_fetch_assoc(mysql_query($query));
}

if (!empty($results)) {
  echo exlplode(" ", $results);
}

?>
于 2013-01-25T22:12:15.560 回答