Executing a SQL script file from PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Marjeta
    New Member
    • Sep 2008
    • 25

    Executing a SQL script file from PHP

    I'm a beginner with PHP, so I apologize if I'm asking something that's a common knowledge.

    I know how to execute a single MySQL command and process its output. But what I'd like to do is to pass a sequence of commands to MySQL and let it execute the entire script and only pass back any potential output of the script.

    Kind of, like what this line does from shell:
    Code:
    mysql -u userName -p < /path/to/file.sql
    I'm using a version of MySQL that doesn't support stored procedures, so I'm looking for ways to get around this problem.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    You could try executing that exact line using exec.

    You could also try the mysqli_multi_qu ery method.

    Comment

    • Marjeta
      New Member
      • Sep 2008
      • 25

      #3
      I looked into the mysqli_multi_qu ery() method, and it's a part of the mysqli class, which has its own connect. The description says it needs
      Originally posted by http://php.net/mysqli_multi_qu ery

      A link identifier returned by mysqli_connect( ) or mysqli_init()
      Our code uses mysql_connect() to connect to database. Unless I change everything else, can I make mysqli_multi_qu ery() work?
      Last edited by Marjeta; Sep 23 '08, 02:31 PM. Reason: fixed a typo

      Comment

      • Marjeta
        New Member
        • Sep 2008
        • 25

        #4
        I also found that:
        In order to have these functions available, you must compile PHP with support for the mysqli extension.

        And, of course, it requires MySQL version 4.1 or more. We are still on 4.0... Ugh!!!

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Ugh indeed. I feel your pain.
          You should go yell at the people in charge of that... There must be some law against torturing developers with old software :P

          To use the mysqli functions you need a mysqli connection. The old MySQL extension and the new Improved MySQL extension (mysqli) don't work together.

          Unfortunately there is no equivalent in the old mysql extension, so you would be forced to either execute it via a shell command or to parse the file into individual statements and execute them one at a time.

          Comment

          Working...