This tutorial explains how to replicate in your remote server the database structure you created on your localhost.
A good way is to create a PHP file that executes SQL queries to create database tables and relationships. In this previous post we have seen how to define the relationships-entities model (tables, attributes, and relationships) for our database to be used in a simplified del.icio.us-like web application:
Step 1: add a DB folder into the site structure
In the site root we create a new folder DB and add a file create_database.php into this folder.
Step 2: create the php file
Open create_database.php on Dreamweaver and switch to Code View. Copy and paste this code inside the <body> tag.
<?php
//Connect to database
include('../config.php');
// CREATE TABLE USER
$sql= "CREATE TABLE USER (
id_user_pk INT NOT NULL AUTO_INCREMENT,
nick VARCHAR(40),
email VARCHAR(40),
password VARCHAR(20),
user_reg_date DATE,
PRIMARY KEY (id_user_pk)
) TYPE=INNODB";
mysql_query($sql);
// CREATE TABLE SITE
$sql="CREATE TABLE SITE (
id_site_pk INT NOT NULL AUTO_INCREMENT,
site_url VARCHAR(250),
site_description LONGTEXT,
site_data_reg DATA,
PRIMARY KEY
) TYPE=INNODB";
mysql_query($sql);
// CREATE TABLE SHARE
$sql="CREATE TABLE SHARE (
id_share_pk INT NOT NULL AUTO_INCREMENT,
id_user INT NOT NULL,
id_site INT NOT NULL,
submitted_by INT NOT NULL DEFAULT 0,
PRIMARY KEY (id_share_pk),
FOREIGN KEY (id_user) REFERENCES USER(id_user_pk) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (id_site) REFERENCES SITE(id_site_pk) ON UPDATE CASCADE ON DELETE CASCADE
) TYPE=INNODB";
mysql_query($sql);
// Close Connection
mysql_close($db_con);
?>
Now save the file and test it on localhost. Remember to include in create_database.php, connection's info (config.php) to your database.
In this way we can reuse create_database.php to copy your database structure on remote server when our project is ready to be published.
No comments:
Post a Comment