Troubleshooting "unexpected T_ENCAPSED_AND_WHITESPACE" when calling mysql_query()
I've made this simple logout script:
<?php
session_start();
$db_connect = mysql_connect('localhost', 'root', '*****');
if(!$db_connect)
{
die('Не може да се осъществи връзка с базата данни' . mysql_error());
}
mysql_select_db("chat", $db_connect);
mysql_query("DELETE FROM activeusers WHERE 开发者_StackOverflow中文版au_id = '$_SESSION['UserId']'");
mysql_close($db_connect);
session_unset();
session_destroy();
?>
But when I put session_unset()
and session_destroy()
at the end my editor shows an error with the mysql_query
I haven't tried this yet but I think that probably written this way I empty the $_SESSION array() and thus $_SESSION['UserId']
is destroyed before the query.Am I right here and how should I do it right?
Format your mysql_query
-command like this:
mysql_query("DELETE FROM activeusers WHERE au_id = '".$_SESSION['UserId']."'");
This makes sure it is properly embedded into the SQL-part of the command.
change your query to:
mysql_query("DELETE FROM activeusers WHERE au_id = '".$_SESSION['UserId']."'");
If you want to inline the variable in the string, you should do one of these:
// Enclose the variable in curly braces:
mysql_query("DELETE FROM activeusers WHERE au_id = '{$_SESSION['UserId']}'");
// Remove quotes from the element name:
mysql_query("DELETE FROM activeusers WHERE au_id = '$_SESSION[UserId]'");
See http://php.net/language.types.string#language.types.string.parsing for more information on how PHP interprets variables in strings.
精彩评论