create connection php code

6
1) CREATE THE CONNECTION SETTINGS FILE We make an external PHP file and then include it so we don't have to paste the same thing in multiple pages or edit a bunch of pages if our connection settings change. This is a BIG deal if you have a huge site and suddenly have to change your password or something. connect.php PHP Code: <?php $server = ""; // aka "localhost" or "69.249.73.42" $username = ""; // aka "ChuckNorris" $password = ""; // aka "cankickyourass" $database = ""; // where you want your new table to go mysql_connect("$server", "$username", "$password") or die(mysql_error()); mysql_select_db("$database") or die(mysql_error()); ?> 2) CREATE THE DATABASE TABLE "FILES" A) fid - file id, set at auto-increment so the database will automatically know to go to the next ID number. I typically name my ID's with whatever is being added to the table...like for users, I use 'uid', etc. B) name - whatever the file is named. C) type - MIME type. D) content - the actual file content. You can add other fields if you want to tag more information onto the file record. On one of my sites, I have fields to track the category it's listed under, the person that uploaded it, and the date it was uploaded. To keep it simple, we're just going to do the basic stuff. You might be a little confused asto how the file is physically stored to a database. What happens is the file's content is put into the content field through the BLOB (Binary Large Object) type. It's a part of the database now. The BLOB type we're going to use is MEDIUMBLOB which will allow up to 16 MB files. If you're thinking "HOLY COW! I don't want people to be uploading 16 MB files!" stress not my friend. We're going to throttle the file size and type later. You can add this table in two ways... A) a MySQL GUI (aka phpMyAdmin): Go into phpMyAdmin > your database > click the "SQL" tab > paste this: Code: CREATE TABLE `files` ( `fid` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'unique id', `name` VARCHAR( 30 ) NOT NULL COMMENT 'file name',

Upload: haftamu-hailu

Post on 16-Dec-2015

218 views

Category:

Documents


4 download

DESCRIPTION

establishing connection in php

TRANSCRIPT

1) CREATE THE CONNECTION SETTINGS FILEWe make an external PHP file and then include it so we don't have to paste the same thing in multiple pages or edit a bunch of pages if our connection settings change. This is a BIG deal if you have a huge site and suddenly have to change your password or something.

connect.phpPHP Code:

2) CREATE THE DATABASE TABLE "FILES"A) fid - file id, set at auto-increment so the database will automatically know to go to the next ID number. I typically name my ID's with whatever is being added to the table...like for users, I use 'uid', etc.B) name - whatever the file is named.C) type - MIME type.D) content - the actual file content.

You can add other fields if you want to tag more information onto the file record. On one of my sites, I have fields to track the category it's listed under, the person that uploaded it, and the date it was uploaded. To keep it simple, we're just going to do the basic stuff.

You might be a little confused asto how the file is physically stored to a database. What happens is the file's content is put into the content field through the BLOB (Binary Large Object) type. It's a part of the database now. The BLOB type we're going to use is MEDIUMBLOB which will allow up to 16 MB files. If you're thinking "HOLY COW! I don't want people to be uploading 16 MB files!" stress not my friend. We're going to throttle the file size and type later.

You can add this table in two ways...A)a MySQL GUI (aka phpMyAdmin):Go into phpMyAdmin > your database > click the "SQL" tab > paste this:Code:CREATE TABLE `files` (`fid` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'unique id',`name` VARCHAR( 30 ) NOT NULL COMMENT 'file name',`type` VARCHAR( 30 ) NOT NULL COMMENT 'MIME type',`size` INT( 11 ) NOT NULL COMMENT 'file size',`content` MEDIUMBLOB NOT NULL COMMENT 'actual file') ENGINE = MYISAM COMMENT = 'Uploaded files'B)or through a PHP file on your server:addtable.phpPHP Code: