connecting-to-mysql-server-using-php-

PHP provides several functions to perform DML operation on MySql database. These functions allow to establish connection, retrieve data and update existing data. Some of the most commonly used functions are

•    mysql_connect (“hostname”, “user”, “pass”): Connects to the MySQL server.
•    mysql_select_db(“database name”): Equivalent to the MySQL command USE; makes the selected database the active one.
•    mysql_query(“query”): Used to send any type of MySQL command to the server.
•    mysql_fetch_rows(“results variable from query”): Used to return a row of the entire results of a database query.
•    mysql_fetch_array(“results variable from query”): Used to return several rows of the entire results of a database query.
•    mysql_error(): Shows the error message that has been returned directly from the MySQL server.
In following example we will look at first three functions.
This example creates a Mysql database using PHP.

<?php
//connect to MySQL; note we’ve used our own parameters- you should use
//your own for hostname, user, and password
$connect = mysql_connect(“localhost”, “root”, “pass”) or
die (“Error , check your server connection.”);
//create the main database if it doesn’t already exist
$create = mysql_query(“CREATE DATABASE IF NOT EXISTS SCHOOL”)
or die(mysql_error());
//make sure our recently created database is the active one
mysql_select_db(“school”);
//create “student” table
$stud = “CREATE TABLE student (
stud_id int(11) NOT NULL auto_increment,
stud_name varchar(255) NOT NULL,
stud_class varchar(255) NOT NULL,
PRIMARY KEY (stud_id))”;
$results = mysql_query($stud) or die (mysql_error());
echo “School Database successfully created!”;
?>
How it works
Firstly we used following command to establish connection with Mysql Server
$connect = mysql_connect(“localhost”, “root”, “pass”) or
die (“Error, check your server connection.”);
this function uses three parameters
First parameters represent Hostname

Host name: In our case, it’s the local host because i have installed everything locally. You will need to change this to whatever host is acting as your MySQL server.

Second and third parameters are for user name and password

Username and password: I have used root user for connections you may use either root or any other user.

Following function call create database if not exists.
$create = mysql_query(“CREATE DATABASE IF NOT EXISTS SCHOOL”)
or die(mysql_error());
mysql_query() function execute query on the database.
After creating a database we must use following command to select it
in order to use
mysql_select_db(“school”);
Then again used mysql_query() to create a table name “student”;
In next article we well see how to retrieve data from this database.
NOTE
While executing this script if you encounter following error

Fatal error : Call to undefined function mysql connect() …..

This means that you have not configured PHP to work with Mysql.

If you have installed PHP from installation package then you missed the Mysql extension installation.

If you have downloaded zipped version of PHP then make sure to uncomment following extension in PHP.INI file.

extension=php_mysql.dll3

to uncomment an extension simply remove leading semi colon (;).
You may find following article helpful

http://programming-web.com/installation-of-apache-php-and-mysql/

Comments are closed.