Run a whole sql code with php? menu

User Tag List

Results 1 to 6 of 6
  1. #1
    runiker's Avatar Contributor
    Reputation
    105
    Join Date
    Nov 2007
    Posts
    501
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Run a whole sql code with php?

    Is there a way to run a sql code (a .sql file thats found on website as well) with php what i want is to be able to click a button and then it run the code.

    Run a whole sql code with php?
  2. #2
    Sirupsen's Avatar Member
    Reputation
    25
    Join Date
    Oct 2007
    Posts
    260
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Remember, before doing this - connect to MySQL first.

    PHP Code:
    $sql 'CREATE TABLE `Sample Tabel` ('
            
    ' `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, '
            
    ' `something1` VARCHAR(255) NOT NULL, '
            
    ' `something2` TEXT NOT NULL, '
            
    ' `something3` LONGTEXT NOT NULL, '
            
    ' `time` TIMESTAMP NOT NULL'
            
    ' )'
            
    ' ENGINE = myisam;'
    Is our SQL code. To execute this SQL code from PHP, run it in a SQL Query:

    PHP Code:
    $insert mysql_query($sql); 
    So, the entire code would look like this obviously

    PHP Code:
    $sql 'CREATE TABLE `Sample Tabel` ('
            
    ' `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, '
            
    ' `something1` VARCHAR(255) NOT NULL, '
            
    ' `something2` TEXT NOT NULL, '
            
    ' `something3` LONGTEXT NOT NULL, '
            
    ' `time` TIMESTAMP NOT NULL'
            
    ' )'
            
    ' ENGINE = myisam;';

    $insert mysql_query($sql); 
    Remember to connect to your database before doing this!


    So, to execute the stuff when you click a button, try something like this (not tested)

    PHP Code:
    <form method="post" name="form" action="?create=1">
    <input type="submit" name="submit" value="Create">
    </form>
    <?
    if ($_GET["create"] == "1") {
    $sql = 'CREATE TABLE `Sample Tabel` ('
            . ' `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, '
            . ' `something1` VARCHAR(255) NOT NULL, '
            . ' `something2` TEXT NOT NULL, '
            . ' `something3` LONGTEXT NOT NULL, '
            . ' `time` TIMESTAMP NOT NULL'
            . ' )'
            . ' ENGINE = myisam;';

    $insert = mysql_query($sql);

    echo "Sucessfully added the table.";
    }
    ?>
    Last edited by Sirupsen; 05-17-2009 at 11:55 AM.



  3. #3
    Blazeme's Avatar Member
    Reputation
    11
    Join Date
    Jul 2008
    Posts
    15
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With mySQL query function (mysql_query) you can only run ONE query, for example you cannot run:
    mysql_query("INSERT ... VALUES (1,223); Update ... SET a=b WHERE c=1");
    To run multiple queryes you need to do explode() and then foreach.
    Of course, to explode you need to have some kind of seprarator (you can use ; for this one), no new lines (str_replace("\n","",$sql) and etc.

  4. #4
    1337php's Avatar Member
    Reputation
    13
    Join Date
    May 2006
    Posts
    46
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you can I would just download SQLYog, make a mysql user that you can connect to remotely and just execute the batch. You could also just upload phpmyadmin to the server and execute it that way. PMA has a file limit though and it tends to screw up sometimes.

  5. #5
    visitor's Avatar Contributor
    Reputation
    174
    Join Date
    Mar 2008
    Posts
    307
    Thanks G/R
    16/15
    Trade Feedback
    1 (100%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sirupsen:
    that's not what runiker wanted
    1337php:
    in php. not sqlyog
    Hey I just met you

  6. #6
    runiker's Avatar Contributor
    Reputation
    105
    Join Date
    Nov 2007
    Posts
    501
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually this is kinda want i needed i wanted to make a page that runs an sql in order to make some gm tools work and stuff like that kinda like if you ever used phpbb3 how they have there set up button.

Similar Threads

  1. [Help] SQL Trouble with my backup
    By marcicompita in forum World of Warcraft Emulator Servers
    Replies: 6
    Last Post: 03-04-2008, 12:37 PM
  2. Help with PHP
    By mkultra. in forum Community Chat
    Replies: 4
    Last Post: 12-31-2007, 08:48 AM
  3. Help needed - how to insert .sql files with SQLyog
    By ADAMZY in forum World of Warcraft Emulator Servers
    Replies: 3
    Last Post: 11-23-2007, 05:32 PM
All times are GMT -5. The time now is 01:56 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search