![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 16
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
[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]
__________________
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2005
Posts: 16
Rep Power: 0
![]() |
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; |
|
|
|
|
|
#4 |
|
Programming Guru
![]() |
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? |
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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()); |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|