Page 1 of 2
Install Something To Database
Posted: Wed Jul 31, 2002 10:26 am
by Dale
Something a bit different.
Heres what needs to be entered into the database:
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?
Posted: Wed Jul 31, 2002 10:29 am
by llimllib
Code: Select all
$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.
Posted: Wed Jul 31, 2002 10:31 am
by Dale
I'll try that now

Posted: Wed Jul 31, 2002 10:35 am
by Dale
Parse error: parse error, unexpected T_VARIABLE in /var/www/html/dale/forum/install.php on line 13
Line 13:
$query2= " CREATE TABLE topics (
Whole INSTALL.PHP:
Code: Select all
<?
$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');
?>
Posted: Wed Jul 31, 2002 10:43 am
by llimllib
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.
Posted: Wed Jul 31, 2002 10:48 am
by Dale
Sorry but im a n00b at this.
INSTALL.PHP:
Code: Select all
<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password")
or die("Could not connect");
print ("Connected successfully");
mysql_close($link);
?>
<?
$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');
?>
Is that right?
Posted: Wed Jul 31, 2002 11:10 am
by llimllib
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:
Code: Select all
<?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);
?>
Posted: Wed Jul 31, 2002 11:34 am
by Dale
Right put that in and filled in User & Pass now it said this:
Connected successfully
error creating table 1
So its connected to the database correctly but now there is something wrong with table 1 for it not to be adding itself to the database, right?
Posted: Wed Jul 31, 2002 12:21 pm
by llimllib
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.
Posted: Wed Jul 31, 2002 1:40 pm
by Dale
ok...trying now...
Posted: Thu Aug 01, 2002 4:30 pm
by Dale
ok it now says:
No Database Selected
Posted: Thu Aug 01, 2002 4:59 pm
by llimllib
look up the function
mysql_select_db; read the examples in the manual; you're getting closer.
Posted: Thu Aug 01, 2002 5:00 pm
by Dale
hmmm man this is hard...im only 14 and i know some people are younger but man this is hard but at the same time im getting closer.
Posted: Thu Aug 01, 2002 5:03 pm
by Dale
ok that bit i dont understand.
bool mysql_select_db ( string database_name [, resource link_identifier])
Posted: Thu Aug 01, 2002 5:10 pm
by llimllib
ok, here's how you read a function definition:
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.