我有一个新闻系统应该在用户更改其个人资料时动态更新用户数据。这是脚本。
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$query = "SELECT * FROM news";
$data = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($data);
while ($row = mysqli_fetch_array($data)) {
$user_id = $row['user_id'];
$title = $row['title'];
$post = $row['post'];
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$query2 = "SELECT * FROM user WHERE user_id = $user_id";
$data2 = mysqli_query($dbc, $query2);
$row = mysqli_fetch_array($data2);
echo '<div class="viewpost">';
echo '<div class="vpside">';
if(!empty($row['picture'])) {
echo '<img class="pictest" src="' . MM_UPLOADPATH . $row['picture'] . '" alt="' . MM_UPLOADPATH . 'nopic.png' . '" />';
}
if(!empty($row['username'])) {
echo '<p>UserName:<br />' . $row['username'] . '</p>';
}
if(!empty($row['rank'])) {
echo '<p>Rank:<br />' . $row['rank'] . '</p>';
}
if(!empty($row['gender'])){
echo '<p>Gender:<br /> ' . $row['gender'] . '</p>';
}
echo '</div>';
if(!empty($title)) {
echo'<h4><u>' .$title. ' in ' .$category. ' News Updates</u></h4>';
}
if(!empty($post)) {
echo '<pre><p class="">' . $post. '</p></pre>';
}
echo '</div><br />';
}
echo'</div>';
我不确定它为什么不起作用,但我认为它可能与循环有关。它也只显示了三个新闻帖子中的两个。这两个新闻帖是由同一个人写的,所以我认为它可能只是拉动了一个用户。
答案 0 :(得分:0)
以下是使用INNER JOIN
<?php
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$query = "SELECT news.title, news.post, news.category, user.picture, user.username, user.rank, user.gender FROM news INNER JOIN user ON user.user_id = news.user_id";
$data = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($data)) {
echo '<div class="viewpost">';
echo '<div class="vpside">';
if(!empty($row['picture'])) {
echo '<img class="pictest" src="' . MM_UPLOADPATH . $row['picture'] . '" alt="' . MM_UPLOADPATH . 'nopic.png' . '" />';
}
if(!empty($row['username'])) {
echo '<p>UserName:<br />' . $row['username'] . '</p>';
}
if(!empty($row['rank'])) {
echo '<p>Rank:<br />' . $row['rank'] . '</p>';
}
if(!empty($row['gender'])){
echo '<p>Gender:<br /> ' . $row['gender'] . '</p>';
}
echo '</div>';
if(!empty($row['title'])) {
echo'<h4><u>' . $row['title'] . ' in ' . $row['category'] . ' News Updates</u></h4>';
}
if(!empty($row['post'])) {
echo '<pre><p class="">' . $row['post']. '</p></pre>';
}
echo '</div><br />';
}
echo'</div>';
?>