简体   繁体   中英

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. 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; check the manual that corresponds to your MySQL server version for the right syntax to use near '.' at line 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:

$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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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