1

I have this function:

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>"; 
 } 
   }

it works if I assign:

 $mainpassion = 'some skill';

What I would like to do is randomly choose 5 'skills' from my table 'skills':

+----------+-----------+
|skill_id  | skill_name|
+----------+-----------+
|        1 |   guitar  |  
|        2 |   cooking |   
|        3 |   math    |    
|        4 |   plumbing|
|        5 |   piano   |
+----------+-----------+

and then get 5 users pictures for each skill. I tried this but it doesn't show anything:

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>';
}
6
  • Have you tried running the query manually? Commented Oct 8, 2012 at 6:20
  • yes, if I do $mainpassion = 'guitar' and then userPicBySkill($mainPassion)...it works Commented Oct 8, 2012 at 6:22
  • You say "it doesnt show anything" - if you view source, can you see maybe the empty img tag? Or is that not being shown either? Commented Oct 8, 2012 at 6:23
  • i think the problem is because of same $row in both functions. Try to change them. Commented Oct 8, 2012 at 6:25
  • these functions are both in same class or just two separate functions?? Commented Oct 8, 2012 at 6:26

3 Answers 3

3

It's better to apply joins instead of doing 25 queries:

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
Sign up to request clarification or add additional context in comments.

1 Comment

you're right. i have to use join in this case! +1 for your advice. thanks
0

use different $row variable in function like this, this is causing problem.

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>"; 
 } 
}

5 Comments

-1. $row is only used once within the function scope and according to the question is called from another function .. the two scopes having nothing in common.
@Jack I just changed that and now it works. I agree with you that the $row should be used only within the function scope...that's why i didn't think it could be the problem
@mattia You actually had both blocks of code in the same scope then?
@Jack yes,that was the problem
@mattia That's completely contrary to your question then, because it shows the top function being called by the below; and as such, it's impossible that such a local variable name has any effect.
0

What do you mean by 'doesn't show anything'? Is the list of 5 skill names being returned?

To get 5 random skills, try $skill_list = mysql_query("SELECT DISTINCT(skill_name) FROM skills ORDER BY RAND() LIMIT 5");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.