Saturday, August 30, 2008

Connect Mysql database in php

PHP main target is to make possible build dynamic web sites.
This possibility is available by using some database system to store information and manipulate them, when system and user needs it.
On PHP a large number of database systems come by default embbed as libraries.
On this article we will learn how to use one of the most used on data handling - MySQL.
MySQL can be installed easly, by downloading EasyPHP.
This application installs Mysql on a web based version, it means that you can manage all database processes on your browser.
It allows to create databases, and tables, edit fields , backup tables system database and a large option of new features.
To access the interface that allows you to create database click with the right button of mouse on tray icon "Administration".
Right next click


it will open the window where you must the name of your database and next it will ask for your table name.

Check how many fields you need on your table and build it.

Now the database and table are created let´s configure the conection to the mysql server using PHP.
Remember that server is configured with an default username and password.
You can make secure by changing it at your choice.

Follows the PHP code generated to build the table

CREATE TABLE `admin` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(40) NOT NULL,
`password` varchar(16) NOT NULL,

and now time to connect with php this table.....

$con = mysql_connect("localhost","database usernam","password");
if (!$con)
die('Could not connect: ' . mysql_error());

No comments: