MySQL查询从表中获取主键(customer_id)并将其与当前会话id(customer_id)进行比较


MySQL query to get the primary key(customer_id) from a table and to compare it with the current session id(customer_id)

运行此代码时出现此错误。

解析错误:语法错误,C:'xampp'htdocs'pta'cc11205'book .php on line 32中意外的'" "' (T_CONSTANT_ENCAPSED_STRING)

 $querycust = mysql_query(" SELECT * FROM customer WHERE customer_id = ".$_SESSION['customer_id']" ");

tq .

您的查询有语法错误,请将查询更改为

$querycust = mysql_query(" SELECT * FROM customer WHERE customer_id = '".$_SESSION['customer_id']."' ");

字符串中缺少连接符号。为了清楚起见,我宁愿将会话存储在一个变量中,例如

$session_var = $_SESSION['customer_id'];
$querycust = mysql_query(" SELECT * FROM customer WHERE customer_id = $session_var");

也考虑使用PDOMySQLI扩展来防止SQL Injection

转义。

" SELECT * FROM customer WHERE customer_id = '".$_SESSION['customer_id']'" "