how to run a sql file using php

Discussion in 'PHP' started by shadow_boi, Dec 24, 2008.

  1. #1
    Hi.

    I have a .sql file which contains about 200 lines of queries.

    I know i can go to mysqlAdmin and import those queries to my newly created database.

    I am wondering, is there a way to run all those queries using php codes?

    I know i can do mysql_query(parameter), but I dont know how to bump all queries from my file into that parameter, so that i only need to use mysql_query method once.

    thanks in advance.
     
    shadow_boi, Dec 24, 2008 IP
  2. farad

    farad Peon

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Why you need to spend time on creating such a thing, if you can do this in a few minutes through the cpanel ? :eek:
     
    farad, Dec 25, 2008 IP
  3. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #3
    I have never made one. I have looked at a lot of install scripts. Other than making the DB connection and setting up the query they are nothing more than a sql dump.

    Here are two fragments, clearly labeled that do the same thing (different DB).

    A sample from a php install script:

    $q1 = "DROP TABLE IF EXISTS `tbl_name` ";
    mysql_query($q1) or die(mysql_error()." at line ". __LINE__);
    
    $q1 = "CREATE TABLE `tbl_name` (
      `BannerID` smallint(5) NOT NULL auto_increment,
      `BannerFile` varchar(255) NOT NULL,
      `BannerURL` varchar(255) NOT NULL,
      `AltText` varchar(150) NOT NULL,
      `BannerType` char(4) NOT NULL,
      PRIMARY KEY  (`BannerID`)
    ) AUTO_INCREMENT=5 ";
    mysql_query($q1) or die(mysql_error()." at line ". __LINE__);
    
    //-- Dumping data for table `tbl_name`
    
    $q1 = "INSERT INTO `tbl_name` VALUES (1, '1156033691_dsd_banner.gif', 'http://www.somesite.com/', 'Some text', 'Full')";
    mysql_query($q1) or die(mysql_error()." at line ".__LINE__);
    $q1 = "INSERT INTO `tbl_name` VALUES (2, '1154984206_hs.gif', 'http://www.hotscripts.com/?RID=N357500', 'The Net´s largest script repository', 'Full')";
    mysql_query($q1) or die(mysql_error()." at line ".__LINE__);
    $q1 = "INSERT INTO `tbl_name` VALUES (3, '1156033723_dsd_bnr_sml.gif', 'http://somesite.com/', 'Some text', 'Menu')";
    PHP:


    A sample from a sql dump that would import via phpmyadmin:

    DROP TABLE IF EXISTS `categories`;
    CREATE TABLE `categories` (
      `id` int(4) NOT NULL auto_increment,
      `category` varchar(255) NOT NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=110 ;
    
    -- 
    -- Dumping data for table `categories`
    -- 
    
    INSERT INTO `categories` VALUES (1, 'Aardvark jokes');
    INSERT INTO `categories` VALUES (2, 'Accountant jokes');
    INSERT INTO `categories` VALUES (3, 'Answer me this jokes');
    PHP:


    As you can plainly see there is very little difference.

    Merry Christmas
     
    Colbyt, Dec 25, 2008 IP
  4. justinlorder

    justinlorder Peon

    Messages:
    4,160
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Don't you know phpmyadmin are made of many php scripts/files .
     
    justinlorder, Dec 25, 2008 IP
  5. www.hunthost.in

    www.hunthost.in Peon

    Messages:
    147
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    www.hunthost.in, Dec 25, 2008 IP
  6. shadow_boi

    shadow_boi Peon

    Messages:
    374
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    shadow_boi, Dec 25, 2008 IP