PHP Database Integration With MySQL

Started by Kalyan, Dec 18, 2008, 05:28 PM

Previous topic - Next topic

Kalyan

PHP Database Integration With MySQL

One of the defining features of PHP is the versatility it offers for connection to, and manipulation with, databases. In this article, we look at some features of the PHP and MySQL combination. We shall go through the following steps:-

    * Connect to MySQL Database Server
    * Create new Database
    * Select MySQL Database
    * Add data to table
    * Retrieve data
    * Error Handling

Connect to MySQL Database Server:
To work with the MySQL database, we first need to connect to MySQL Database Server. PHP provides mysql_connect() function to do this, and requires three strings as input: 'hostname', 'username' and 'password'. Use Code-1 to connect to MySQL Database Server.

Code 1: test.php
<html>
<head>
  <title>Server test Page </title>
</head>
<body>
  <p>Testing working of MySQL.</p>
  <?php
   /* Connecting to MySQL */
   $link = mysql_connect("mysql_host", "mysql_user", "mysql_password") or die("Could not connect : " . mysql_error());
   print "Connected successfully";
  ?>
</body>
</html>

If your script has no error, then Code-1 gives output as:

Output 1: test.php
Testing working of MySQL.
Connected successfully

source : Techxcel