ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

PHP Program: Connect/Config PHP to MY SQL Database

Updated on April 9, 2013

There are only 3 easy Steps on how to make and connect your PHP program to your MYSQL database

To connect your database to the program you are making you must configure it first, so that when you are adding, editing, deleting or viewing pieces of information from the database you can successfully do it by calling the database name and its certain table. To do it, run your php server and code it in any php editor. As for me, I use XAMPP as the server and notepad++ as the editor.

3 Steps to connect to the database

1. First create a database using mysql standard. How to create a database using mysql? To create a database using mysql, see the syntax and sample code below:

Syntax:

CREATE DATABASE yourDatabaseName

Example:

CREATE DATABASE userInfo

Note: It is not necessarily that the define functions are in upper cases, you can also write it in lowercase. Capitalizing the built in function is for emphasis as well as for clarity.

Just replace yourDatabaseName with any name you want for your database. If you want to create a database with XAMPP. Just open the XAMPP, run the APACHE and MYSQL, once you did that, go to your browser and type: LOCALHOST/PHPMYADMIN, it will direct you to XAMPP Window Interface. CLICK the SQL button, then create your database on the BLANK FORM with the syntax above, then click GO. Once you successfully did that, your database will be displayed on the left side bar of the browser. For more clarification, see the pictures below this hub.

2. Once you have your database, you must create a table or fields to your database that will hold the information. How to make a database table? To make a database table, see the syntax and sample code below.

Syntax:

CREATE TABLE yourTableName

(

…code the column name here

)

Example:

CREATE TABLE userInformation

(

IDNo int primary key not null auto_increment,

FirstName text not null,

MiddleName text not null,

LastName text not null,

)

To make table on your database through XAMPP, just double click your database name on the left side bar of your browser, then click SQL button then make the table by following the example above. Then click go, if you successfully made a table It will also display on the left side bar of your browser under your database name. (See Photos below this hub)

3. Lastly, to connect your database to the program you are making, go to your editor and follow the syntax below.

Syntax:

<?php

$server = 'localhost';

$user = 'root';

$pass = "";

$database = 'yourDatabaseNameHere';

// Connect your Database

$connectDatabase = mysql_connect($server, $user, $pass)

or die ("Could not connect to server ... \n" . mysql_error ());

//select the database you want to connect with

mysql_select_db($database)

or die ("Could not connect to database ... \n" . mysql_error ());

?>

Example:

<?php

$server = 'localhost';

$user = 'root';

$pass = "";

$database = 'userInfo';

// Connect your Database

$connectDatabase = mysql_connect($server, $user, $pass)

or die ("Could not connect to server ... \n" . mysql_error ());

//select the database you want to connect with

mysql_select_db($database)

or die ("Could not connect to database ... \n" . mysql_error ());

?>

See the Photos for Clarification of the Steps

Creating the Database

This is the interface shown after typing localhost/phpmyadmin on the address bar , click the mys sql button
This is the interface shown after typing localhost/phpmyadmin on the address bar , click the mys sql button
source: aisha91.hubpages.com
source: aisha91.hubpages.com

Creating the My SQL Table

Source: aisha91.hubpages.com
Source: aisha91.hubpages.com

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      jeet 3 years ago

      it's very good

    Click to Rate This Article