Student Of Fortune

PHP Connection To MySQL

Share on :
What should be prepared to learn this MySQL PHP programming:
1. PHP Program
2. Apache Web Server
3. MySQL Database
4. Editor (Ex: Macromedia Dreamweaver, EditPlus, etc.)
5. MySQL Editor (Ex: phpMyAdmin, MySQL Front, etc.)
To program PHP - Apache - MySQL - phpMyAdmin, many programs that have been packaged distribution that contains the 3 programs above. This means that with one time install, then we will automatically tersinstall to 3 programs above, for example PHPTriad, AppServ, Vertrigo, Xampp, Wamp and many others.

I'll explain from the beginning, ranging from installation to how to display MySQL data in Browser.

1. Installation
Please download the distribution PHP packages - Apache - MySQL - phpMyAdmin. I use a 2:10 Vertrigo distribution available on the web http://sourceforge.org with the following specifications:
- Apache 2.0.59
- PHP 5.1.4
- MySQL 5.0.24
- PhpMyAdmin 2.8.2.1
To the editor, I use Macromedia Dreamweaver MX 2004, and sometimes I'll use EditPlus fast,
2. Creating Databases and Tables
Before creating tables in MySQL database, ensure that all Service running (Apache). I normally use phpMyAdmin to create databases, create tables, fill the tables and so on (if there is an easy, why bother.) Create a database, for example databse with the name of the project. To access phpMyAdmin http://localhost/phpmyadmin type in the browser.

CREATE DATABASE project;

Then create a table named users table

     CREATE TABLE users (
     username VARCHAR (32) NOT NULL,
     password VARCHAR (32) NOT NULL,
     full_name VARCHAR (50) NOT NULL,
     PRIMARY KEY (username)
     )

Enter a minimum of two records (data) to the users table

     INSERT INTO users (username, password, full_name) VALUES
     ('Supono', 'm45ter', 'Supono Thea'),
     ('ayu', 'm4h4d3w1', 'Siti Rahayu');

3. PHP connection to MySQL
Open your favorite editor, immediately wrote the script type the following:

<? Php
    / *
    * Connection to MySQL
    * Script: mysql_connect ("host", "username", "password");
    * /
    
$ Conn = mysql_connect ("localhost", "root", "vertrigo");
    if ($ conn) {/ / => same as if ($ conn == true)
    echo "Connect to Server successfully <br />";
    } Else {
    echo "Connection to Server Failed <br />";
    exit; / / => program will not execute the next code (out)
    }

    
/ *
    
* Select database
    * Script: mysql_select_db ("nama_database");
    * /
    
$ Select_db = mysql_select_db ("Project");
    if ($ select_db) {
    echo "Selecting successful database <br />";
    } Else {
    
echo "Selecting Database Failed <br />";
    exit;
    
}

    
/ *
    
* Show data
    * Query to the database script: mysql_query ("SQL statement");
    * Grab data script: mysql_fetch_array ("Query Results");
    * /
    
$ Sql = "SELECT * FROM users"; / / SQL command to display all data
    $ Res = mysql_query ($ sql); / / The command queries to the database
    while ($ row = mysql_fetch_array ($ res)) {/ / Looping over the existing data
    echo "Name: $ row [full_name] <br />"; / / display the data in column (field) full_name
    }
    
?>
 

0 comments on PHP Connection To MySQL :

Post a Comment and Don't Spam!

Dont Spam please

 
Recommended Post Slide Out For Blogger

Recent Comments

My Rank