我试图从PHP执行此查询但它无效:
$myQuery = 'SET @t1= 5;
INSERT INTO mvt (id_piece, code_mvt, origine_mvt, type_mvt, article_id_article, code_art, des_art, qte_old, qte_mvt, qte_new, link_piece)
SELECT
p.id_bsr,
CONCAT_WS(\'\',\'MVT/15/12/\',LPAD(@t1 := @t1+1, 3, \'0\')) AS code_mvt,
p.code_bsr,
\'Bon Annulation\',
l.article_id_article,
l.code_art,
l.des_art,
qte_art,
qte,
(qte_art + (qte*1)),
\'index.php?p=DetailsBonSortie&idBsr=167\'
FROM
bon_sortie p, ligne_sortie l, article a
WHERE p.id_bsr = l.bon_sortie_id_sortie
AND a.id_article = l.article_id_article
AND id_bsr = 167;
';
您可以看到来自INSERT
查询和变量SELECT
的{{1}}查询。
当我直接在MySQL数据库中执行此查询时,它工作正常,但当我尝试从PHP执行它时这样:
@t1
它不起作用!
我错过了什么?
答案 0 :(得分:4)
您不能使用php-mysql扩展名
在同一语句中执行2个查询试试这个:
$myQuery1 = 'SET @t1=5';
$myQuery2 = '
INSERT INTO mvt (id_piece, code_mvt, origine_mvt, type_mvt, article_id_article, code_art, des_art, qte_old, qte_mvt, qte_new, link_piece)
SELECT
p.id_bsr,
CONCAT_WS(\'\',\'MVT/15/12/\',LPAD(@t1 := @t1+1, 3, \'0\')) AS code_mvt,
p.code_bsr,
\'Bon Annulation\',
l.article_id_article,
l.code_art,
l.des_art,
qte_art,
qte,
(qte_art + (qte*1)),
\'index.php?p=DetailsBonSortie&idBsr=167\'
FROM
bon_sortie p, ligne_sortie l, article a
WHERE p.id_bsr = l.bon_sortie_id_sortie
AND a.id_article = l.article_id_article
AND id_bsr = 167;
';
$conn = new mysqli('host', 'user', 'password', 'database');
$conn->query($myQuery1);
$conn->query($myQuery2);