CREATE TABLE posts (
ID int(5) DEFAULT '0' NOT NULL auto_increment,
TopicID int(5) DEFAULT '0' NOT NULL,
Name varchar(50) NOT NULL,
Email varchar(50) NOT NULL,
Password varchar(50) NOT NULL,
TimeStamp varchar(10) NOT NULL,
Post text NOT NULL,
PRIMARY KEY (ID)
);
CREATE TABLE topics (
ID int(5) DEFAULT '0' NOT NULL auto_increment,
TopicName varchar(50) NOT NULL,
PRIMARY KEY (ID)
);
Now i wanna make a install.php page which will automatically place them in the database how will i do that?
$query1= " CREATE TABLE posts (
ID int(5) DEFAULT '0' NOT NULL auto_increment,
TopicID int(5) DEFAULT '0' NOT NULL,
Name varchar(50) NOT NULL,
Email varchar(50) NOT NULL,
Password varchar(50) NOT NULL,
TimeStamp varchar(10) NOT NULL,
Post text NOT NULL,
PRIMARY KEY (ID)
)"
$query2="CREATE TABLE topics (
ID int(5) DEFAULT '0' NOT NULL auto_increment,
TopicName varchar(50) NOT NULL,
PRIMARY KEY (ID)
)"
$result = mysql_query($query1) or die('error creating table 1');
$result = mysql_query($query2) or die('error creating table 2');
//I assume you're already connected to the DB with this code.
Ok, first you're going to need to connect to the database, like I said in the comment in the code listing. use mysql_connect() followed by mysql_select_db().
Finally, do some debugging! I forgot to put in semicolons after the strings in my code sample, so add them in there.
Don't apologize! just keep trying - it'll come. You want to read this tutorial before you continue, it will show you how to connect to mysql. I'm not going to explain my code fixes, I want you to figure them out. What I will say is that every statement needs to end in a ';', but mysql queries should not. Here's the revised code:
<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password")
or die("Could not connect");
print ("Connected successfully");
$query1= " CREATE TABLE posts (
ID int(5) DEFAULT '0' NOT NULL auto_increment,
TopicID int(5) DEFAULT '0' NOT NULL,
Name varchar(50) NOT NULL,
Email varchar(50) NOT NULL,
Password varchar(50) NOT NULL,
TimeStamp varchar(10) NOT NULL,
Post text NOT NULL,
PRIMARY KEY (ID)
)"; //semicolons go outside the quotes
$query2= " CREATE TABLE topics (
ID int(5) DEFAULT '0' NOT NULL auto_increment,
TopicName varchar(50) NOT NULL,
PRIMARY KEY (ID)
)";
$result = mysql_query($query1) or die('error creating table 1');
$result = mysql_query($query2) or die('error creating table 2');
mysql_close($link);
?>
you are correct, now you're starting to debug. Try die(mysql_error()) instead of die("query 1 failed") - that should tell you what mysql's having a problem with.
the first part, bool, is the return value type. What that means, in this case, is that the function will return a value of true or false. mysql_select_db returns true on success and false on failure, as the description tells you.
next comes the function name, mysql_select_db
now the arguments to the function. first, a string database_name, which signifies, well, the name of the database. second, a resource link_identifier, which is the thing that is returned by the mysql_connect statement. Because the second arg is in brackets, it is an optional argument. As the text explains, if you omit it, the function will use the most recent connection, which should be just fine by you.