我尝试执行此查询:
$q = "SET @userId := (SELECT user_id
FROM users
WHERE user_email='$userEmail'
AND user_token='$userToken' );
SELECT stock_name, stock_description, stocks.stock_id, stock_owner_id,
stock_creation_date, stock_last_mod_date, stock_num_item_to_buy,
stock_distance_to_buy, stock_num_allowed_items, tab.user_role
FROM stocks
INNER JOIN
(SELECT stock_id, user_role
FROM user_stock
WHERE user_id = @userID) tab
ON tab.stock_id=stocks.stock_id
WHERE stocks.stock_id='$stockId'
UNION ALL
SELECT * , 'owner' as user_role
FROM stocks
WHERE stock_owner_id=@userId AND stocks.stock_id='$stockId';";
使用:
$mysqli->query ( $q );
但结果是这个错误:
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 'SELECT stock_name, stock_description, stocks.stock_id,
stock_owner_id, st' at line 5
如果我尝试使用 PhpMyAdmin 或 MySQLWorkbrench 的相同查询,一切正常......
...但是通过 PHP 我仍然有这个错误。我也尝试使用:
$mysqli->multi_query ( $q );
但结果是一样的(老实说,在这种情况下我也无法收到错误消息)。
如果我删除第一部分(变量的 SET),查询也可以通过 PHP 正常工作,我无法理解问题是什么!!!
请帮我!
======================= 编辑========================== ========================感谢@suchit,我刚刚看到了var名称的错误,但纠正错字后还是一样!
我看到如果我使用 ; 在 SQL 语句中,PHP 的工作方式与 PhpMyAdmin 不同,其中相同的查询工作正常,但如果我使用 mysqli_multi_query 问题仍然存在。
唯一的解决方案是执行@Barmar 所说的两个不同的查询,谢谢!
我只会补充: mysqli_multi_query 有什么用?!我查看了手册,但我看到它的用途是进行更多查询,但为什么对我不起作用?