How to insert/create stored procedures in mySQL from PHP?

后端 未结 3 1272
被撕碎了的回忆
被撕碎了的回忆 2021-01-02 22:05

I\'ve got a number of stored procedures made with the MySQL Workbench. They work just fine when use MySQL workbench to put them into my test DB.

Now I am preparing

相关标签:
3条回答
  • 2021-01-02 22:32

    DELIMITER is something that the mysql client application actually uses. I believe that the client application is responsible for breaking up the queries that it sends to Mysql. That is what PHPMyAdmin does, for example.

    Instead of spending a whole night writing a script to parse MySQL into queries, use the code I wrote. You will find it in the scriptToQueries function, here:

    http://wush.net/svn/luckyapps/pie/trunk/framework/classes/Db/Mysql.php

    ENJOY

    EDIT: Since writing this answer I have ported the code into the free Q platform in which you can study the code: https://github.com/EGreg/Platform/blob/master/platform/classes/Db/Mysql.php#L824

    0 讨论(0)
  • 2021-01-02 22:35

    I haven't tested, but I won't be surprised by mysqli_multi_query() expecting to have the same delimiter of each queries. Try to pack the stored procedure creation in a single query, without using the DELIMITER modifier ?

    So instead of

    <?php
    $results = mysqli_multi(
        'DELIMITER $$
        USE `dbname`$$
        CREATE PROCEDURE `procname`(IN inputparameters)
        BEGIN
        ... procedure goes here
    
        ;
        END$$
        DELIMITER ;
    ');
    ?>
    

    Just do this

    <?php
    $result = mysqli_query('CREATE PROCEDURE `procname`(IN inputparameters) BEGIN ...; END');
    

    And tell us if it works :)

    0 讨论(0)
  • 2021-01-02 22:35

    To sum it up:

    DELIMITER is implemented client-side, not serverside.

    If you have a good driver or API, it may take care of this. PHP mysql / mysqli, as of now, do not.

    If you need to use a different delimiter (e.g. because the default delimiter appears inside scripts), you have to encode/escape your SQL yourself or break it up so you don't need to change the delimiter. No help from PHP here.

    0 讨论(0)
提交回复
热议问题