0

I'm trying to execute this query from PHP but it's not working:

$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;
                           ';

As you can see it's an INSERT query from a SELECT query and a variable @t1.
When I execute this query directly in MySQL DB it works fine, but when I try to execute it from php like this:

$conn = new mysqli('host', 'user', 'password', 'database');
$conn->query($myQuery);

it doesn't work!
What am I missing ?

5
  • 3
    "it doesn't work!" - Not much to go on here. Check for errors php.net/manual/en/mysqli.error.php --- php.net/manual/en/function.error-reporting.php Commented Dec 7, 2015 at 13:57
  • 5
    You can't execute multiple queries with $conn->query() Commented Dec 7, 2015 at 13:57
  • 1
    Hard to tell without the actual error - try echo $myQuery before running, might be an obvious syntax issue. Commented Dec 7, 2015 at 14:01
  • 2
    Look for mysqli multi query Commented Dec 7, 2015 at 14:01
  • @Mihai thnx, (mysqli multi query) saved the day :) Commented Dec 7, 2015 at 14:09

1 Answer 1

4

You can't execute 2 queries in the same statement with the php-mysql extension

Try this:

$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);
Sign up to request clarification or add additional context in comments.

3 Comments

+1, I usually do something like $queries = explode(";", $sql); foreach($queries as $q); $conn->query($q);
That's a good solution when you have a lot of queries (and without ';' in them). However, I believe it is safer to write the queries one by one (possibly in an array, which you can loop over with foreach), than to explode them from a big string. You might run into problems. (Delimiters can be changed, there can be ';' characters in strings in your query, ...)
@RonnyCoolen thnx for the explanation, this make sence :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.