2010-08-15 133 views
3

我有2個表(Users,Wall)。 Wall表中的UserID是一個外鍵。我將如何去使用此獲取用戶的詳細信息? (我想獲取用戶的名字及姓氏誰發佈的消息。)MySQL + PHP:使用外鍵獲取數據

用戶表: alt text http://i33.tinypic.com/1eq6n5.png

牆表: alt text http://i37.tinypic.com/b5po5u.png

編輯:我無法弄清楚如何顯示的數據。

<?php include('config.php'); ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html> 
    <head> 
     <title>Alpha</title> 
     <link rel="stylesheet" href="style.css" type="text/css" /> 
    </head> 
    <body> 

<?php 

// Logged IN 
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Email'])) { 


// Post to Database 
if(!empty($_POST['message'])) 
{ 
$message = mysql_real_escape_string($_POST['message']); 
$postmessage = mysql_query("INSERT INTO Wall (Message, UserID) VALUES('".$message."', '".$_SESSION['UserID']."')"); 
} 

// Collet Latest Posts 

$result = mysql_query('SELECT Message, UserID 
         FROM Wall 
        ORDER BY MessageID DESC 
         LIMIT 20') or die('Invalid query: ' . mysql_error()); 

// Collet Post User 

$query = mysql_query('SELECT Forename, Surname FROM Users INNER JOIN Wall ON Users.UserID = Wall.UserID;') or die('Invalid query: ' . mysql_error()); 

    ?> 
    <div id ="container"> 
     <div id="insideleft"> 
      <ul> 
       <li><a href="index.php">Home</a></li> 
       <li><a href="profile.php">Edit Profile</a></li> 
       <li><a href="wall.php">Community Wall</a></li> 
       <li><a href="logout.php">Logout</a></li> 
      </ul> 
     </div> 
     <div id="insideright"> 
      <h1>Community Wall</h1> 
      <br /> 
      <form method="post" action="wall.php" name="wallpost" id="wallpost"> 
       <label for="message" class="message">Message: </label> <input type="text" name="message" id="message" class="message"/> 
       <input type="submit" name="messagesub" id="messagesub" value="Post" /><br /><br /> 
       </fieldset> 
      </form> 
      <?php while ($row = mysql_fetch_assoc($result)) { ?> 
      <p></p> 
      <p><?=stripslashes($row['Message'])?></p><br /> 

<?php 
} ?> 

     </div> 
    </div> 
    <?php 
} 

//else {echo "<meta http-equiv='refresh' content='0;index.php'>";} 

?> 
</body> 
</html> 

正如你所看到的我正在輸出消息,但我不知道如何輸出海報的姓和名。

+4

請不要問一個新問題。繼續通過編輯您的舊的問題:http://stackoverflow.com/questions/3489017/mysql-php-fetching-data-using-foreign-keys – silvo 2010-08-15 20:41:53

回答

8
$hostname = 'localhost'; 
$username = 'username'; 
$password = 'password'; 
$dbname = 'database'; 

$db = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password); 

$query = <<<QUERY 
    SELECT Forename, Surname 
    FROM Users 
    INNER JOIN Wall ON Users.UserID = Wall.UserID; 
QUERY; 

$statement = $db->query($query); 
$rows = $statement->fetch(PDO::FETCH_ASSOC); 
print_r($rows); 

$db = null; 

編輯: 鑑於新的信息,您應在查詢合二爲一。

<?php include('config.php'); ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html> 
    <head> 
     <title>Alpha</title> 
     <link rel="stylesheet" href="style.css" type="text/css" /> 
    </head> 
    <body> 

<?php 

// Logged IN 
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Email'])) { 


// Post to Database 
if(!empty($_POST['message'])) 
{ 
$message = mysql_real_escape_string($_POST['message']); 
$postmessage = mysql_query("INSERT INTO Wall (Message, UserID) VALUES('".$message."', '".$_SESSION['UserID']."')"); 
} 

// Collet Latest Posts 

$query = <<<QUERY 
    SELECT Users.UserID, Message, Forename, Surname 
    FROM Users 
    INNER JOIN Wall ON Users.UserID = Wall.UserID; 
    ORDER BY MessageID DESC 
    LIMIT 20; 
QUERY; 
$result = mysql_query($query) or die('Invalid query: ' . mysql_error()); 

// Collet Post User 
    ?> 
    <div id ="container"> 
     <div id="insideleft"> 
      <ul> 
       <li><a href="index.php">Home</a></li> 
       <li><a href="profile.php">Edit Profile</a></li> 
       <li><a href="wall.php">Community Wall</a></li> 
       <li><a href="logout.php">Logout</a></li> 
      </ul> 
     </div> 
     <div id="insideright"> 
      <h1>Community Wall</h1> 
      <br /> 
      <form method="post" action="wall.php" name="wallpost" id="wallpost"> 
       <label for="message" class="message">Message: </label> <input type="text" name="message" id="message" class="message"/> 
       <input type="submit" name="messagesub" id="messagesub" value="Post" /><br /><br /> 
       </fieldset> 
      </form> 
      <?php while ($row = mysql_fetch_assoc($result)) { ?> 
      <p></p> 
      <p> 

      <?php 
       echo "Message: ".stripslashes($row['Message'])."<br />"; 
       echo "Name: {$row['Surname']}, {$row['Forename']}"; 
      ?> 

      </p><br /> 

<?php 
} ?> 

     </div> 
    </div> 
    <?php 
} 

//else {echo "<meta http-equiv='refresh' content='0;index.php'>";} 

?> 
</body> 
</html> 
+0

+1爲完整答案與PHP源代碼:) – silvo 2010-08-15 20:17:12

+0

感謝您的幫助到目前爲止,但是我不知道如何輸出當前正在打印的消息的數據。 – ritch 2010-08-15 20:30:33

+0

再次歡呼,我收到以下消息「無效查詢:不是唯一表格/別名:'Wall'」 – ritch 2010-08-15 20:46:05

0
select w.MessageID, w.Message, u.UserID, u.Forename, u.Surname 
from Wall w 
inner join Users u on w.UserID = u.UserID 
0

或以另一種形式:

SELECT w.*, u.Forename, u.Surname 
FROM Wall w, Users u 
WHERE w.UserID=u.UserID 
+0

感謝您的幫助,但是我不知道如何輸出當前正在打印的信息。 – ritch 2010-08-15 20:31:04

1

由於這裏的主要目標是消息您在SQL查詢中第一個與消息表寫在你的榜樣牆表和查詢可以看起來像這個: $result = mysql_query ("SELECT u.Forename, u.Surname, w.Message FROM Wall AS w INNER JOIN Users AS u ON(w.UserID=u.UserID)");

現在,當輸出使用:

<p><?=stripslashes($row['Surname'])?></p><br /> 
<p><?=stripslashes($row['Lastname'])?></p><br /> 
<p><?=stripslashes($row['Message'])?></p><br /> 

當然,做正確的格式。

+0

嗨,謝謝。我正在使用Zurahn提供的代碼,但是我在執行時得到以下輸出:「無效查詢:不是唯一表/別名:'Wall'」 對此有何幫助?乾杯 – ritch 2010-08-15 20:59:41

+0

好吧,我改變了查詢,看看。 – Centurion 2010-08-15 21:21:01