[英]Relate MySQL table to a specific value
我想做的事情很簡單:我想從數據庫中恢復數據,只有第一個表(存儲在會話中)的ID與第二個表的ID相似。 我的代碼如下。 唯一的問題:當前代碼給我以下錯誤:
查詢無效:您的SQL語法有誤; 請查看與您的MySQL服務器版本相對應的手冊,以獲取在'。'附近使用的正確語法。 在第1行
我究竟做錯了什么? 謝謝你的幫助!
<?php include "base.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
//We check if the user is logged
if(isset($_SESSION['Username']))
{
?>
<?php
//query
$query = mysql_query("select TitreEvent, DescriptionEvent from users_event where ID = .$_SESSION [id].") or die ('Query is invalid: ' . mysql_error());
//write the results
while ($row = mysql_fetch_array($query)) {
echo $row['TitreEvent'] . " " . $row['DescriptionEvent'] . "";
// close the loop
}
?>
<div class="message">To access this page, you must be logged.<br />
<a href="http://www.groupe90.webege.com/index.php">Log in</a></div>
<?php
}
?>
<div class="foot"><a href="<?php echo $url_home; ?>">Go Home</a> - <a href="http://www.webestools.com/">Webestools</a></div>
</body>
</html>
您的PHP在構建查詢字符串的位置已損壞:
$query = mysql_query("select [...snip...] where ID = .$_SESSION [id].") or die ('Query is invalid: ' . mysql_error());
^-- here
您永遠不會結束字符串,因此$ _SESSION部分實際上是查詢字符串的一部分,產生了
where ID = Array [id].
它應該是
where ID = " . $_SESSION['id']) ...
$query = mysql_query("select TitreEvent, DescriptionEvent from users_event where ID = '$_SESSION[id]'") or die ('Query is invalid: ' . mysql_error());
試試上面的代碼
您必須將條件放在單引號中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.