使user_id等于用户名

时间:2014-01-10 18:04:53

标签: php mysql

我在php中有以下代码

// Get all the questions 
$query = mysqli_query($con, "SELECT * FROM da_questions INNER JOIN da_users ORDER BY da_questions.question_id DESC");

$allquestions = array(); 

while($row = mysqli_fetch_array($query))
{
    $allquestions[] = $row;
}

如何使user_id中的table da_questions等于user_id中的da_users,以便在执行以下操作时,我会获得正确用户ID的正确用户名。

<?php foreach($allquestions as $question): ?>

    <span><b><?php echo $question['question_title']; ?><span class="pull-right"><?php echo $question['username']; ?></span></b></span>
    <hr style="margin-top:0px;" />
    <p><?php echo $question['question_text']; ?></p>

<?php endforeach; ?>

1 个答案:

答案 0 :(得分:1)

您的加入中需要ON条件

SELECT * 
FROM da_questions 
INNER JOIN da_users ON da_users.user_id = da_questions.user_id
ORDER BY da_questions.question_id DESC