phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


MySQL Connection: How to connect with a database server?

Tag: mysql, database   Category: Mysql
post: 31 Oct 2007 read: 1,059


This is basic tutorial about connection to MySQL database server. I will show to you how to connect to you database server for first time.

You can use mysql database connection with mysql_connect() function. For Example, like below


<?php 
   $link = mysql_connect("localhost", "mysql_user", "mysql_password") 
       or die("Could not connect: " . mysql_error()); 
   print ("Connected successfully"); 
   mysql_close($link); 
?>l;

mysql_close() function is optional. The link to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling mysql_close().

little more deep:

mysql_connect() establishes a connection to a MySQL server. The following defaults are assumed for missing optional parameters: server = 'localhost:3306', username = name of the user that owns the server process and password = empty password.

Another option, you can use mysql_pconnect(). mysql_pconnect() acts very much like mysql_connect() with two major differences.

First, when connecting, the function would first try to find a (persistent) link that's already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.

Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use (mysql_close() will not close links established by mysql_pconnect()).

The optional client_flags parameter became available in PHP 4.3.0.




| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting