Connecting Tech Pros Worldwide Forums | Help | Site Map

Build_DB PHP script

Angelos
Guest
 
Posts: n/a
#1: Jul 17 '05
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($sql) 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_levels`;
CREATE TABLE `cms_access_levels` (
`access_lvl` int(10) unsigned NOT NULL auto_increment,
`access_lvl_name` varchar(45) NOT NULL default '',
PRIMARY KEY (`access_lvl`,`access_lvl_name`)
) TYPE=MyISAM;

/*!40000 ALTER TABLE `cms_access_levels` DISABLE KEYS */;
INSERT INTO `cms_access_levels` VALUES (1,'user'),
(2,'moderator'),
(3,'admin');
/*!40000 ALTER TABLE `cms_access_levels` 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_passwd` varchar(50) NOT NULL default '',
`cms_user_name` varchar(45) NOT NULL default '',
`cms_user_surname` varchar(45) NOT NULL default '',
`cms_user_access_lvl` tinyint(4) unsigned NOT NULL default '1',
`cms_customer_id` int(10) unsigned NOT NULL default '0',
PRIMARY KEY
(`cms_user_id`,`cms_user_email`,`cms_user_access_l vl`,`cms_customer_id`)
) TYPE=MyISAM;
************************************************** *************************************

Thanks Again
Angelos..



P.S. Gamoto !



Simon
Guest
 
Posts: n/a
#2: Jul 17 '05

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


Angelos
Guest
 
Posts: n/a
#3: Jul 17 '05

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 ;-)


Simon
Guest
 
Posts: n/a
#4: Jul 17 '05

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


Angelos
Guest
 
Posts: n/a
#5: Jul 17 '05

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


Simon
Guest
 
Posts: n/a
#6: Jul 17 '05

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


Angelos
Guest
 
Posts: n/a
#7: Jul 17 '05

re: Build_DB PHP script


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

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

$sql = '
DROP TABLE IF EXISTS cms_access_levels ;
CREATE TABLE IF NOT EXISTS cms_access_levels (
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($sql) 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_levels ( 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 ???


Closed Thread