Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 10th, 2005, 6:07 AM   #1
stakeknife
Newbie
 
Join Date: Mar 2005
Posts: 16
Rep Power: 0 stakeknife is on a distinguished road
Loading an SQL script in PHP

Hi all,

First thanks to those who helped with my previous problem... (the one with functions and forms, got it working nicely 2, ill post up when it looks a little more decent).

I am trying to load an SQL script from a file called sql.php, this document starts like this

<?php
header("location:../index.php");
exit;
?>

DROP DATABASE core;
CREATE DATABASE core;
USE DATABASE core;

CREATE TABLE core_article
(
article_id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(120) NOT NULL,
subtitle VARCHAR(250) NOT NULL,
author_id INT NOT NULL,
cat_id INT NOT NULL,
body TEXT NOT NULL,
enabled SMALLINT NOT NULL,
timestamp INT NOT NULL,
PRIMARY KEY (article_id),
FOREIGN KEY (author_id) REFERENCES core_users(user_ID),
FOREIGN KEY (cat_id) REFERENCES core_article_category(article_categoryID)
)TYPE=MyISAM;

and continues on in that format...

from my installation php file i wrote this piece of code

$filename = "admin/sql.php";
@$fd = fopen ($filename, "r");
$sql_data = @fread($fd, filesize($filename));
@fclose ($fd);

$conn=mysql_connect($_POST['mysql_server'], $_POST['mysql_user'], $_POST['mysql_pass']);
mysql_query($sql_data);
mysql_close();

I have two problems if the DB 'core' already exists it doesnt drop it...
If i manually drop the 'core' DB in command prompt and run the script it appears to work creating the DB 'core'. But if i try and view the tables created by the install script i get a nice friendly

Error 12: Can't read dir of '.\core\' (Errcode: 2)

Am i writing this code right or have i done something totally wrong?

Thanks in advance
stakeknife is offline   Reply With Quote
Old Mar 10th, 2005, 9:46 PM   #2
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 6 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
[php]<?php
function getSqlQueries($file) {
$file = file($file); // Weird line, huh? But it works!
$file = exlode(";", $file);
return $file;
}

mysql_connect($_POST['mysql_server'], $_POST['mysql_user'], $_POST['mysql_pass']);

foreach(getSqlQueries("admin/sql.php") as $col => $val)
mysql_query($val) or die(mysql_error());

mysql_close();
?>[/php]
__________________

tempest is offline   Reply With Quote
Old Mar 11th, 2005, 5:18 AM   #3
stakeknife
Newbie
 
Join Date: Mar 2005
Posts: 16
Rep Power: 0 stakeknife is on a distinguished road
thanks for that but i get a strange error when i try to access my sql script, please not this script works fine when loaded into mysql outside of PHP.

I receive this 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 'Array' at line 1

This is the code im using to access it

function getSqlQueries($file)
{
$file = file($file); // Weird line, huh? But it works!
$file = explode(";", $file);
return $file;
}

mysql_connect($_POST['mysql_server'], $_POST['mysql_user'], $_POST['mysql_pass']) or die (mysql_error());

foreach(getSqlQueries('admin/sql.php') as $col => $val)
mysql_query($val) or die(mysql_error());

mysql_close();

and this is the sql.php script

DROP DATABASE core;
CREATE DATABASE core;
USE core;

CREATE TABLE core_article
(
article_id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(120) NOT NULL,
subtitle VARCHAR(250) NOT NULL,
author_id INT NOT NULL,
cat_id INT NOT NULL,
body TEXT NOT NULL,
enabled SMALLINT NOT NULL,
timestamp INT NOT NULL,
PRIMARY KEY (article_id),
FOREIGN KEY (author_id) REFERENCES core_users(user_ID),
FOREIGN KEY (cat_id) REFERENCES core_article_category(article_categoryID)
)TYPE=MyISAM;

CREATE TABLE core_article_category
(
article_categoryID INT NOT NULL AUTO_INCREMENT,
category_name VARCHAR(200),
PRIMARY KEY (article_categoryID)
)TYPE=MyISAM;

CREATE TABLE core_users
(
user_ID INT NOT NULL AUTO_INCREMENT,
user_name VARCHAR(200) NOT NULL,
user_password VARCHAR(20) NOT NULL,
user_email VARCHAR(100) NOT NULL,
user_join INT,
user_class INT,
PRIMARY KEY (user_ID),
FOREIGN KEY (user_class) REFERENCES core_user_classes(class_id)
)TYPE=MyISAM;

CREATE TABLE core_user_classes
(
class_id INT NOT NULL AUTO_INCREMENT,
class_name VARCHAR(200) NOT NULL,
PRIMARY KEY (class_id)
)TYPE=MyISAM;

CREATE TABLE core_comments
(
comment_id INT NOT NULL AUTO_INCREMENT,
comment_pid INT NOT NULL,
comment_subject VARCHAR(200) NOT NULL,
comment_body TEXT NOT NULL,
comment_author VARCHAR(200) NOT NULL,
comment_author_email VARCHAR(200) NOT NULL,
comment_disabled TINYINT(3) NOT NULL,
PRIMARY KEY (comment_id),
FOREIGN KEY (comment_pid) REFERENCES core_article(article_id)
)TYPE=MyISAM;

CREATE TABLE core_upload
(
upload_id INT NOT NULL AUTO_INCREMENT,
upload_name VARCHAR(200) NOT NULL,
upload_category INT NOT NULL,
upload_description TEXT NOT NULL,
upload_datestamp INT,
PRIMARY KEY (upload_id),
FOREIGN KEY (upload_category) REFERENCES core_upload_cat(upload_catid)
)TYPE=MyISAM;

CREATE TABLE core_upload_cat
(
uploadcat_id INT NOT NULL AUTO_INCREMENT,
uploadcat_name VARCHAR(200) NOT NULL,
PRIMARY KEY (uploadcat_id)
)TYPE=MyISAM;

CREATE TABLE core_forum_topics
(
topic_id INT NOT NULL AUTO_INCREMENT,
topic_title VARCHAR(150) NOT NULL,
topic_timestamp datetime,
topic_author INT,
PRIMARY KEY (topic_id),
FOREIGN KEY (topic_author) REFERENCES core_users(user_ID)
)TYPE=MyISAM;

CREATE TABLE core_forum_posts
(
post_id INT NOT NULL AUTO_INCREMENT,
post_tid INT NOT NULL,
post_body TEXT,
post_timestamp datetime,
post_author INT,
PRIMARY KEY (post_id),
FOREIGN KEY (post_author) REFERENCES core_users(user_ID),
FOREIGN KEY (post_tid) REFERENCES core_forum_topics(topic_id)
)TYPE=MyISAM;
stakeknife is offline   Reply With Quote
Old Mar 14th, 2005, 4:16 AM   #4
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
change the name of the file you are using to .sql or soemthing .php it might not like and be trying to render the page in php?

andi cant see the word array in any of the sql code, have you checked by doing it manually into a database that the sql is correct?
Berto is offline   Reply With Quote
Old Mar 14th, 2005, 1:37 PM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Your foreach loop is broken somehow. I've never used them, so I don't know what the problem is, but I would do it like this:
$sql = getSqlQueries('admin/sql.php');
for ($i = 0; $i < count($sql); $i++)
    mysql_query($sql[$i]) or die(mysql_error());
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:50 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC