Friday, May 22, 2009

Making connection with MySQL server in php

The beauty of PHP programming is its easy way of estabishing a connection with the database.

Following code snippet shows how to connect to mysql database in windows environment.

// my username is root and password is blank.

$con=mysql_connect("localhost",''root"," ");

if (!$con)

die ("Unable to establish a connection");

/*now we connect to database.

My database name is mydb */

$db=mysql_select_db("mydb");

if (!$db)

die ("Database not found");

// now proceed with building query.

?>

Feel free to ask if you have any questions.


How to redirect to another page from a php page


In php, you can easily redirect a visitor from one page to another apge. The following code snippet is used for this redirection:

header("Location:somepage.php");

// This will redirect the user to somepage.php

?>