PHP simple : PHP tutorial, MySQL tutorial
PHP Tutorial - Free PHP Script

Home | Tutorials | PHP & MySQL Resources | Web Tools
Introduction & Get Start
Using Form
PHP
PHP Tags
PHP Variables
PHP Control Structures
MySQL
Create Database & Table
Database Connection
Insert Record
Select Record
Update Record
Delete Record

MySQL : Database Connection (MySQL Connect)

When you need to use MySQL database from your PHP file. You must connect to your MySQL on your web server, select the database you want and then make the command for browse your records, update, add or delete them. There are the database management commands called SQL language (Structure Query Language).

Connect MySQL Database

Do it first when you need to use database on your file.

Syntax :

mysql_connect("hostname", "username", "password");

Example
<?
$host="localhost";
// most web server use "localhost" for the default name.
$user="root"; // default username for your machine. (setup from Appserv)
$password=""; // default password for your machine. (setup from Appserv)

mysql_connect($host,$user,$password);

/* you can insert parameters directly into this SQL command as : mysql_connect("localhost","root",""); or put the response command if that connection fail. or die("Can not connect DB"); after the mysql_connect() command.

The new connection code with failing check may be :
mysql_connect($host,$user,$password)or die("Can not connect DB");
*/

?>


Select Database

Select database you want to use.

Syntax :

mysql_select_db("database_name");

Example
<?
// Connection codes with database selection.
$host="localhost";
$user="root";
$password="";


mysql_connect($host,$user,$password);
mysql_select_db("test");
// select database "test" to use.

// You can put or die(); after mysql_connect() and mysql_select_db()


?>


Both connections above should be often to use by many files in one site and you must to change your username and password in all files when you upload to the real server on the net. The best way, put these codes in a file, save it and then use include(); or require(); command in PHP files you want.

Just save the example codes above to "connectdb.php" and when you need to use MySQL connection use include(); command.

Example
<?
include("connectdb.php");
?>



Close Connection

Close your connection at the end of file for free your web server's buffer.

Syntax :

mysql_close();

Example
<?
// Connect database.
include("connectdb.php");

// Your PHP code here...

// Close connection.
mysql_close();
?>



Page Navigator
Create a simple Page Navigator for your PHP programs such as webboard, guestbook, online catalog, product list page, etc.
Easy Counter with Images
Apply your basic counter with digit images. Short and easy codes. This counter plus by 1 every time you refresh your browser.



PHPsimple.net
info@phpsimple.net