繁体   English   中英

将MySQL表关联到特定值

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM