php - 在另一个查询中使用查询中的值

标签 php mysql

我有一个疑问

 $result = mysql_query("SELECT * FROM comprofiler WHERE cb_playstationgames LIKE '%FIFA%' ORDER BY id ASC");

在该查询中有一个 user_id,我需要在另一个表上执行查询。

$gebruikerid = mysql_query("SELECT * FROM users where id LIKE '".$result['user_id']."'");

现在我想在 while 循环中使用该值

echo "<table><tr><th width=\"300\" align=\"left\" >Avatar</th><th width=\"300\" align=\"left\">Naam</th><th width=\"200\" align=\"left\">PSN Naam</th></tr>"; 

while($row2 = mysql_fetch_array($result))
{
 echo "<tr><td><img height=\"50\" width=\"50\" src=\"/images/comprofiler/" . $row2['avatar'] . "\"></td><td>" . $gebruikernaam . "</td><td>" .     $row2['cb_psnnaam'] . "</td></tr>";
}
echo "</table>";   

我无法让查询根据第一个表中的 id 读取另一个表中的值。有人可以帮助我吗?

最佳答案

在您的情况下,最好的解决方案是使用 JOIN。

$result = mysql_query("SELECT comprofiler.*, users.* FROM comprofiler INNER JOIN users ON users.id = comprofiler.user_id WHERE cb_playstationgames LIKE '%FIFA%' ORDER BY comprofiler.id ASC");

您可能必须手动指定要选择的列(在 SELECT 之后),以防两个表中的列名称相同并且您需要两者。

$result = mysql_query("SELECT comprofiler.id as id_comprofiler, users.id as id_user, users.avatar ... " );

关于php - 在另一个查询中使用查询中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20220139/

相关文章:

php - 修改 url 更短的脚本

mysql - 插入后 LAST_INSERT_ID 上的描述符索引无效

PHP 数据对象 (PDO) 示例

MYSQL 选择两个不同的值

php - 在 PHP 的 while 循环中在偶数和奇数之间交替

php - 用于获取viewCount的Youtube API仅在本地有效

PHP mysqli_query 报语法错误

mysql - 尝试删除 1000 个日志条目,但如果只有 999 个可用,我会超时

php - 在 PHP array_walk 中按引用传递第三个参数,没有警告

PHP 读取另一个域上的 cookie