Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

SQL Error displayed by PHP, havent a clue at this point!

Options
  • 11-03-2005 12:57pm
    #1
    Registered Users Posts: 721 ✭✭✭


    I am trying to load information from an SQL file into my database via PHP, but i get a strange error when i try to access the sql script (sql.php), please note 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, $_POST, $_POST) or die (mysql_error());

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

    mysql_close();

    SQL.PHP looks like this:

    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;

    anyone have any ideas?


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    This line here:
    foreach(getSqlQueries('admin/sql.php') as $col => $val)
    mysql_query($val) or die(mysql_error());
    It would seem that each $val is an array, and not a value.
    Try
    foreach(getSqlQueries('admin/sql.php') as $col => $val)
       for($i = 0; $i < count($val); $i++)
            mysql_query($val[$i]) or die(mysql_error());
    


  • Closed Accounts Posts: 113 ✭✭Pinhead


    I'm not familar with SQL in terms of using it with PHP. First of, don't expect anyone here to reap through that bucket of code and answer your question. Comment out all of those CREATE TABLE statements apart from one, then check if you get a syntax error when you try to add the SQL script. If you don't, then that statement is fine. Uncomment another and repeat the process.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Actually, read here: http://ie.php.net/manual/en/function.file.php
    About the correct use of file(). My solution should work, but it's a patch which doesn't address the problem.


Advertisement