php - 将 MySQL 表与特定值相关联

标签 php mysql

我想做的很简单:我想从数据库中恢复数据,只是第一个表(存储在 session 中)的 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']) ...

关于php - 将 MySQL 表与特定值相关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17865577/

相关文章:

php - MySQL 按日期范围过滤查询

MySQL 按字段唯一性对结果进行排序

mysql查询问题WHERE date <

php - mysql sum 的随机结果

php - 使用 laravel dusk 进行登录测试

php - 什么在 PHP 命名空间或类元素中使用

php - 动态创建的 HTML 文件输入元素不发布数据

php MySQL 查询不返回任何内容

mysql - MySQL数据库在哪里存储数据?

php - 匹配问题