I've been pulling my hair out over this for 2 days....
I use the following code to construct a query to create several new tables. The code gives me an SQL error, but when I paste the failed query into phpMyAdmin, it executes perfectly every time. I've tried using addslashes and everything else I can think of and nothing seems to help....
//include user authentication file
require_once("auth.inc.php");
//include config file which contains all config info
//including db_connection info. Since we're using
//PEAR DB functions, we've enabled a nice callback
//function to spit out detailed errors...
require_once("config.inc.php");
//get the user-suppplied prefix for the new tables we will be creating
$db_prefix=$_GET['database_prefix'];
$query="CREATE TABLE IF NOT EXISTS ".$db_prefix."answers(
answer_id int(11) NOT NULL auto_increment,
question_id int(11) NOT NULL default '0',
answer_text text NOT NULL,
PRIMARY KEY (answer_id)
) TYPE=MyISAM;
CREATE TABLE IF NOT EXISTS ".$db_prefix."chapters(
chapter_id int(11) NOT NULL auto_increment,
chapter_number smallint(6) NOT NULL default '0',
chapter_title varchar(255) NOT NULL default '',
course_id smallint(6) NOT NULL default '0',
PRIMARY KEY (chapter_id)
) TYPE=MyISAM;
CREATE TABLE IF NOT EXISTS ".$db_prefix."pages(
page_id int(11) NOT NULL auto_increment,
chapter_id int(11) NOT NULL default '0',
page_title varchar(255) NOT NULL default '',
page_number int(11) NOT NULL default '0',
test_id int(11) NOT NULL default '0',
page_content text NOT NULL,
PRIMARY KEY (page_id)
) TYPE=MyISAM;
CREATE TABLE IF NOT EXISTS ".$db_prefix."question_types(
question_type_id smallint(6) NOT NULL auto_increment,
description varchar(255) NOT NULL default '',
PRIMARY KEY (question_type_id)
) TYPE=MyISAM;
CREATE TABLE IF NOT EXISTS ".$db_prefix."questions(
question_id int(11) NOT NULL auto_increment,
correct_answer_id int(11) NOT NULL default '0',
rationale_page_ids tinytext NOT NULL,
question_type_id int(11) NOT NULL default '0',
question mediumtext NOT NULL,
PRIMARY KEY (question_id)
) TYPE=MyISAM;
CREATE TABLE IF NOT EXISTS ".$db_prefix."students(
student_id int(11) NOT NULL auto_increment,
login tinytext NOT NULL,
status char(2) NOT NULL default '',
session_data text NOT NULL,
PRIMARY KEY (student_id)
) TYPE=MyISAM;
CREATE TABLE IF NOT EXISTS ".$db_prefix."tests(
test_id int(11) NOT NULL auto_increment,
test_question_ids text NOT NULL,
test_type_id smallint(6) NOT NULL default '0',
test_time_limit smallint(6) NOT NULL default '0',
PRIMARY KEY (test_id)
) TYPE=MyISAM;";
$result = $dbh->query($query);
$query="INSERT INTO courses (course_name,course_url,course_start,course_prerequisites,productid,db_prefix) VALUES('".$_GET['course_name']."','".$_GET['course_url']."','".$_GET['course_start']."','".$course_prerequisites."','".$_GET['productid']."','".$db_prefix."');";
$result = $dbh->query($query);
The above code produces the following error:
DB Error: syntax error CREATE TABLE IF NOT EXISTS test6_answers( answer_id int(11) NOT NULL auto_increment, question_id int(11) NOT NULL default '0', answer_text text NOT NULL, PRIMARY KEY (answer_id) ); CREATE TABLE IF NOT EXISTS test6_chapters( chapter_id int(11) NOT NULL auto_increment, chapter_number smallint(6) NOT NULL default '0', chapter_title varchar(255) NOT NULL default '', course_id smallint(6) NOT NULL default '0', PRIMARY KEY (chapter_id) ); CREATE TABLE IF NOT EXISTS test6_pages( page_id int(11) NOT NULL auto_increment, chapter_id int(11) NOT NULL default '0', page_title varchar(255) NOT NULL default '', page_number int(11) NOT NULL default '0', test_id int(11) NOT NULL default '0', page_content text NOT NULL, PRIMARY KEY (page_id) ); CREATE TABLE IF NOT EXISTS test6_question_types( question_type_id smallint(6) NOT NULL auto_increment, description varchar(255) NOT NULL default '', PRIMARY KEY (question_type_id) ) TYPE=MyISAM; CREATE TABLE IF NOT EXISTS test6_questions( question_id int(11) NOT NULL auto_increment, correct_answer_id int(11) NOT NULL default '0', rationale_page_ids tinytext NOT NULL, question_type_id int(11) NOT NULL default '0', question mediumtext NOT NULL, PRIMARY KEY (question_id) ) TYPE=MyISAM; CREATE TABLE IF NOT EXISTS test6_students( student_id int(11) NOT NULL auto_increment, login tinytext NOT NULL, status char(2) NOT NULL default '', session_data text NOT NULL, PRIMARY KEY (student_id) ) TYPE=MyISAM; CREATE TABLE IF NOT EXISTS test6_tests( test_id int(11) NOT NULL auto_increment, test_question_ids text NOT NULL, test_type_id smallint(6) NOT NULL default '0', test_time_limit smallint(6) NOT NULL default '0', PRIMARY KEY (test_id) ) TYPE=MyISAM; [nativecode=1064 ** You have an error in your SQL syntax near '; CREATE TABLE IF NOT EXISTS test6_chapters( chapter_id int(11) NOT NULL auto' at line 6]
but if you paste this query from the error message into phpMyAdmin, it executes fine. Someone please tell me why. Please. I'm begging here. Really