Build_DB PHP script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Angelos

    Build_DB PHP script

    Hello Everybody !!!

    How do you put the folowing code into a php script that will create the
    database and then the Tables ?
    I tried doing
    $sql = '
    CREATE DATABASE /*!32312 IF NOT EXISTS*/ `gamoto`;
    USE `gamoto`;
    ';
    $result = mysql_query($sq l) or die(mysql_error ());

    But i get errors when I execute the query....

    I know the long way of doing it..... (query by query, table by table)
    I need a quicker way.... Thanks guys !!!!
    *************** *************** *************** *************** *************
    CREATE DATABASE /*!32312 IF NOT EXISTS*/ `gamoto`;
    USE `gamoto`;

    DROP TABLE IF EXISTS `cms_access_lev els`;
    CREATE TABLE `cms_access_lev els` (
    `access_lvl` int(10) unsigned NOT NULL auto_increment,
    `access_lvl_nam e` varchar(45) NOT NULL default '',
    PRIMARY KEY (`access_lvl`,` access_lvl_name `)
    ) TYPE=MyISAM;

    /*!40000 ALTER TABLE `cms_access_lev els` DISABLE KEYS */;
    INSERT INTO `cms_access_lev els` VALUES (1,'user'),
    (2,'moderator') ,
    (3,'admin');
    /*!40000 ALTER TABLE `cms_access_lev els` ENABLE KEYS */;

    DROP TABLE IF EXISTS `cms_users`;
    CREATE TABLE `cms_users` (
    `cms_user_id` int(10) unsigned NOT NULL auto_increment,
    `cms_user_email ` varchar(60) NOT NULL default '',
    `cms_user_passw d` varchar(50) NOT NULL default '',
    `cms_user_name` varchar(45) NOT NULL default '',
    `cms_user_surna me` varchar(45) NOT NULL default '',
    `cms_user_acces s_lvl` tinyint(4) unsigned NOT NULL default '1',
    `cms_customer_i d` int(10) unsigned NOT NULL default '0',
    PRIMARY KEY
    (`cms_user_id`, `cms_user_email `,`cms_user_acc ess_lvl`,`cms_c ustomer_id`)
    ) TYPE=MyISAM;
    *************** *************** *************** *************** *************** ************

    Thanks Again
    Angelos..



    P.S. Gamoto !


  • Simon

    #2
    Re: Build_DB PHP script

    > Hello Everybody !!![color=blue]
    >
    > How do you put the folowing code into a php script that will create the
    > database and then the Tables ?
    > I tried doing
    > $sql = '
    > CREATE DATABASE /*!32312 IF NOT EXISTS*/ `gamoto`;
    > USE `gamoto`;[/color]

    Isn't it

    USE gamoto;

    ?[color=blue]
    >
    > Thanks Again
    > Angelos..
    >
    >
    >
    > P.S. Gamoto ![/color]

    Simon


    Comment

    • Angelos

      #3
      Re: Build_DB PHP script

      > Isn't it[color=blue]
      >
      > USE gamoto;
      >
      > ?[/color]

      I don't think that what i had was wrong ...
      And also I am asking how you would do that .. The whole script !
      Thanks Simon ;-)


      Comment

      • Simon

        #4
        Re: Build_DB PHP script

        >> Isn't it[color=blue][color=green]
        >>
        >> USE gamoto;
        >>
        >> ?[/color]
        >
        > I don't think that what i had was wrong ...
        > And also I am asking how you would do that .. The whole script !
        > Thanks Simon ;-)[/color]

        Yes, but the problem is where in the script is your problem?
        Have you tried line by line toi find out where it is wrong?

        If you show use the error been returned we might be able to help more.

        On my system, (phpMyAdmin), the script works fine.

        But running it in a php script it does not because of the quotes, "`".

        CREATE DATABASE `gamoto` ... is not the same as
        CREATE DATABASE 'gamoto'

        in the script it should be

        CREATE DATABASE gamoto

        and
        USE gamoto.

        Simon


        Comment

        • Angelos

          #5
          Re: Build_DB PHP script

          CREATE DATABASE `gamoto` ... is not the same as[color=blue]
          > CREATE DATABASE 'gamoto'
          >
          > in the script it should be
          >
          > CREATE DATABASE gamoto
          >
          > and
          > USE gamoto.[/color]

          Ahhhhh ... I see...
          So that's my problem... I thought that I can have this ` ` quotes in the
          script.
          Because the SQL code I am trying to run through php is an export from
          phpmyadmin or a Buckup from MySQL Admin....

          So I though it should work as it is if you run it correclty...
          But still if PHPmyadmin runs the code... it meens that I can run it some how
          .... :S

          Strange... thanks anyway...
          Until I get an answer I am going to remove this ` ` quotes ;)
          Thanks


          Comment

          • Simon

            #6
            Re: Build_DB PHP script

            > CREATE DATABASE `gamoto` ... is not the same as[color=blue][color=green]
            >> CREATE DATABASE 'gamoto'
            >>
            >> in the script it should be
            >>
            >> CREATE DATABASE gamoto
            >>
            >> and
            >> USE gamoto.[/color]
            >
            > Ahhhhh ... I see...
            > So that's my problem... I thought that I can have this ` ` quotes in the
            > script.
            > Because the SQL code I am trying to run through php is an export from
            > phpmyadmin or a Buckup from MySQL Admin....
            >
            > So I though it should work as it is if you run it correclty...
            > But still if PHPmyadmin runs the code... it meens that I can run it some
            > how ... :S
            >
            > Strange... thanks anyway...
            > Until I get an answer I am going to remove this ` ` quotes ;)
            > Thanks[/color]

            I don't know the answer myself, it is either an error in the way phpMyAdmin
            displays the SQL or the actual character been used in their query.
            But I know that only CREATE and USE cannot have quotes (" and '), all the
            others should work just fine.

            Simon


            Comment

            • Angelos

              #7
              Re: Build_DB PHP script

              That is another piece of code :
              *************** *************** *************** ***
              <?
              ini_set('error_ reporting', E_ALL);
              ini_set("displa y_errors","1"); ?>
              <?
              require_once ('../../Connections/willans.php');

              $create = 'CREATE DATABASE IF NOT EXISTS gamoto';
              $results = mysql_query($cr eate) or die (mysql_error()) ;
              mysql_select_db ("gamoto");

              $sql = '
              DROP TABLE IF EXISTS cms_access_leve ls ;
              CREATE TABLE IF NOT EXISTS cms_access_leve ls (
              access_lvl int(10) unsigned NOT NULL auto_increment,
              access_lvl_name varchar(45) NOT NULL ,
              PRIMARY KEY ( access_lvl , access_lvl_name )
              ) TYPE=MyISAM;
              ';
              $results = mysql_query($sq l) or die (mysql_error()) ;
              if ($results)
              echo "DB Created";
              ?>
              *************** *************** *************** *********

              And that is the Error :

              ############### ###############
              You have an error in your SQL syntax. Check the manual that corresponds to
              your MySQL server version for the right syntax to use near '; CREATE TABLE
              IF NOT EXISTS cms_access_leve ls ( access_lv
              ############### ###############
              For some reason it doesn't display the whole error ... but anyway that is
              not the problem.

              How am I going to make the $sql RUN ???


              Comment

              Working...