Install Something To Database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Install Something To Database

Post 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?
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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.
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

I'll try that now ;)
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post 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'); 
?>
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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.
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post 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?
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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);
?>
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post 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?
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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.
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

ok...trying now...
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

ok it now says:

No Database Selected
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

look up the function mysql_select_db; read the examples in the manual; you're getting closer.
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post 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.
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

ok that bit i dont understand.

bool mysql_select_db ( string database_name [, resource link_identifier])
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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.
Post Reply