Sunday, March 06, 2011

database connection with mysql



Step 1 Check the configuration
To smooth our programming later then the first thing we should do is check the configuration by typing the phpinfo (). To do this you need to create a simple php file that calls the function phpinfo (). Consider the following code:




Write the code in notepad and save it as info.php and place it in the htdocs folder. Then open a browser and type localhost / info.php

Consider the system configuration, especially on the MySQL because this part is very important to know because if this part does not exist we would not be able to work without MySQL.



Step 2 Create Database
In this tutorial I will use the MySQL database named test with a table named user. Look at the code below:

CREATE DATABASE if NOT EXISTS `test`;
USE test;

CREATE TABLE `user` (
`id` int(3) NOT NULL auto_increment,
`user` text character set latin1 NOT NULL,
`pass` text character set latin1 NOT NULL,
`nama` varchar(50) character set latin1 NOT NULL default '',
`status` text character set latin1 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=29 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=29 ;

INSERT INTO `user` VALUES (1, 'admin1', 'rahasia1', 'Nuzuli', 'user');
INSERT INTO `user` VALUES (2, 'admin2', 'rahasia2', 'Fitriadi', 'admin');Langkah 3 - make conection database



As his case with other programming that uses a database, the first thing we must do is connect to the database server. Connecting to MySQL database in PHP requires some information that is:

* hostname
* database username
* password
* database name

To connect to a database first you will connect to the MySQL database and in the next step you must select the database to be used. Remember if we fail then the connection we will not be able to proceed to the next script. Consider the following code:
Code:

$host = "localhost";
$user = "nama_username";
$pass = "tentu";
$db = "tentu";

$koneksi=mysql_connect($host,$user,$pass) or die ("koneksi ke database gagal") ;
$koneksi_database= mysql_select_db($db,$koneksi);

Function mysql_connect () is to establish a connection to MySQL server and if the connection is successful then the next step we can choose the database with the command mysql_select_db ().

Step 4 - Retrieve data from database
Now it's time to make something more interesting. For example, let's try to retrieve data from user table in database test. To do this make a connection to the database and select database test. The next step is to create SQL queries - in this case a SELECT statement - to the database. Now try to get all records from MySQL. SQL statement for this are:

$ get = mysql_query ("SELECT * FROM user");

Now after selecting the table, then how is a way to display the contents of that table? How to use the following code:

$get = mysql_query("SELECT * FROM user");
While ($row=mysql_fetch_object($ambil)) {
$name = stripslashes($row->nama);
Echo "$name";
}
?>

In the above code, I used to take mysql_fetch_object SQLnya results of the query so that the resulting rows will be defined as an object. We also can use other functions such as the following:
1. mysql_fetch_assoc () - Ambil hasil baris sebagai array asosiatif
2. mysql_fetch_row () - Ambil hasil enumerated baris sebagai array
3. mysql_fetch_array () - Ambil hasil baris sebagai array asosiatif, array numerik, else twice

The above code will display all existing data in the user table. How can if we want to display some data alone? We will use the limit to limit the number of display data and let a more regular basis we will use the order by to sort the data based on the ASC (az) or DESC (za).

$ get = mysql_query ("SELECT * FROM user ORDER BY name ASC limit of 0.2");
Basic PHP and MySQL Programming

As we already know about web programming using the database will be able to create a dynamic website content. Examples of what we can see for example the online registration application or news on news websites are kept updated every second.

One website that dynamic programming is to use PHP and MySQL database. Why PHP using MySQL database? Actually use the MySQL database is only because of habit. In addition to MySQL we can also use PostgreSQL, MS Access, Oracle, etc..

In this section I will try to explain how to use PHP and MySQL to create dynamic content and of course I just explain the basics in advance to avoid excessive hehe.

in this case I use the xampp php 5 if you can not please fetching in www.cnet.com search on the search form with the name of xampp 1.7.3

2 comments:

Anonymous said...

wah kurang lengkap tu

Anonymous said...

wah...coolll