简体   繁体   中英

PHP get MYSQL id from on table and foreach inside another table

So I have two tables in my MYSQL database, and the first table I need to get the id for the user and then get the information on this user from the second table.

I need to get 10 random id's from the first table, and then foreach id get the information.

Whats the best way to do this? Im going to then store this information in a xml file so I can have like a quiz about the users for the users.

This is my PHP script:

$friend_id_mysql = mysql_query("SELECT friend_id FROM user_lookup
WHERE user_id = '".mysql_real_escape_string($_GET['user_id'])."'
ORDER BY RAND() LIMIT 0,10");

while($result = mysql_fetch_assoc($friend_id_mysql)) {
    $friend_id = $result['friend_id']; 

        $array = array($friend_id);

        foreach ($array as $value) {
            $friend_info = mysql_query("SELECT friend_first_name, friend_picture_url
            WHERE friend_id = '$value'");

            while($row = mysql_fetch_assoc($friend_info)) {
                //Then input this to a XML file (But this can I find out myself :) )
            }

        }
}

But Im not getting anything, theres seems to be some error. How should I solve this?

Thanks in advance

'friend_info'查询中缺少FROM <tablename>

if both tables are in the same database (or even on the same server) you should use a join get; everything in one query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM