简体   繁体   English

将MySQL表关联到特定值

[英]Relate MySQL table to a specific value

What I want to do is quite simple: I want to recover data from a database, only where the ID of the first table (stored in session) is similar to the ID of the second table. 我想做的事情很简单:我想从数据库中恢复数据,只有第一个表(存储在会话中)的ID与第二个表的ID相似。 My code is this one below. 我的代码如下。 The only problem: the present code gives me the following error: 唯一的问题:当前代码给我以下错误:

Query is invalid: You have an error in your SQL syntax; 查询无效:您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near '.' 请查看与您的MySQL服务器版本相对应的手册,以获取在'。'附近使用的正确语法。 at line 1 在第1行

What am I doing wrong? 我究竟做错了什么? Thanks for your help! 谢谢你的帮助!

<?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>

Your PHP is broken where you build the query string: 您的PHP在构建查询字符串的位置已损坏:

$query = mysql_query("select [...snip...] where ID = .$_SESSION    [id].") or die ('Query is invalid: ' . mysql_error());
                                                    ^-- here           

you never ended the string, so the $_SESSION portion is actually part of the query string, producing 您永远不会结束字符串,因此$ _SESSION部分实际上是查询字符串的一部分,产生了

where ID = Array   [id].

It should be 它应该是

 where ID = " . $_SESSION['id']) ...

$query = mysql_query("select TitreEvent, DescriptionEvent from users_event where ID = '$_SESSION[id]'") or die ('Query is invalid: ' . mysql_error());

Try the above code 试试上面的代码

You have to put your condition in single quote 您必须将条件放在单引号中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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