我有这个功能:
function userPicBySkill($mainPassion){
$query = mysql_query("SELECT id, username, imagename FROM users WHERE mainpassion = '$mainPassion' ORDER BY RAND() LIMIT 5");
while ($row = mysql_fetch_array($query)) {
$uid = $row["id"];
$username = $row["username"];
$imagename = $row["imagename"];
echo "<a href='/$username'> <img src='image/$imagename' width='40' height='41' alt = '$username'></a>";
}
}
如果我分配:
$mainpassion = 'some skill';
我想做的是从我的表'技能'中随机选择5'技能':
+----------+-----------+
|skill_id | skill_name|
+----------+-----------+
| 1 | guitar |
| 2 | cooking |
| 3 | math |
| 4 | plumbing|
| 5 | piano |
+----------+-----------+
然后为每项技能获得5张用户图片。我尝试了这个,但它没有显示任何内容:
function findRandomSkill(){
$skill_list = mysql_query("SELECT skill_name FROM skills ORDER BY RAND() LIMIT 5");
while($row = mysql_fetch_array($skill_list)){
$skill = $row['skill_name'];
echo '<div class="userBySkillDiv">
<h5>'.$skill.'</h5>';
userPicBySkill($skill);
echo'</div>';
}
答案 0 :(得分:3)
最好应用联接而不是执行25次查询:
SELECT id, username, imagename
FROM users
INNER JOIN (SELECT skill_name
FROM skills
ORDER BY RAND()
LIMIT 5
) userskills ON users.mainpassion = userskills.skill_name
ORDER BY RAND()
LIMIT 5
答案 1 :(得分:0)
你的意思是什么'没有显示任何东西'?是否返回了5个技能名单?
获得5项随机技能,
试试$skill_list = mysql_query("SELECT DISTINCT(skill_name) FROM skills ORDER BY RAND() LIMIT 5");
答案 2 :(得分:0)
在这样的函数中使用不同的$row
变量,这会导致问题。
function userPicBySkill($mainPassion){
$query = mysql_query("SELECT id, username, imagename FROM users WHERE mainpassion = '$mainPassion' ORDER BY RAND() LIMIT 5");
while ($row1 = mysql_fetch_array($query)) {
$uid = $row1["id"];
$username = $row1["username"];
$imagename = $row1["imagename"];
echo "<a href='/$username'> <img src='image/$imagename' width='40' height='41' alt = '$username'></a>";
}
}