我是PHP新手,我有一个test project:
我想让代码更高效,因为生成需要一些时间。 非常感谢,Nat
它使用的代码如下:
$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);
?>
答案 0 :(得分: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);
}
?>