PHP Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
PHP Topics
Introduction Introduction
Syntax Syntax
Data Types Data Types
Operators Operators
Control Structures Control Structures
Functions Functions
Pre-defined Function Pre-defined Function
Calendar Functions Calendar Functions
Date and Time Date and Time
Array Functions Array Functions
Array List Array Functions List1
Array Function List Array Functions List2
Math Functions Math Functions
PHP MYSQL Functions PHP Mysql Functions
File Handling File Handling
Error Handling Error Handling
DB Size DB Size
PHP Mail PHP Mail
String Tokens String Tokens
String Functions String Functions
String Functions List String Functions List1
String Functions List2 String Functions List2
Session Functions Session Functions
Cookies Functions Cookies Functions
Form Variables Form Variables
Running PHP from JS Running PHP from JS
Array To JS Array To JS
JS Array Array to PHP
Encryption Encryption
Common Header Common Header
Forums Ask Your Doubts
Scraps More about PHP
Feedback Feedback
 




mysql_connect() Function in PHP


Tutorials »Php »

Topic

What is mysql_connect function in PHP?
How does mysql_connect() works?




Explanation

The mysql_connect() function Open a connection to a MySQL Server.

Syntax

resource mysql_connect ( [string server [, string username
[, string password [, bool new_link [, int client_flags]]]]])

Returns a MySQL link identifier on success, or FALSE on failure.

Connection to Mysql database can be established by using mysql_connect function. This function takes three parameters, first one is hostname then user-id and then password. We can give the port number along with the hostname also.


$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")


The above function will return resource id or false depending on the success of the connection.

Mysql_connect once establish the connection the link will be present till the script execution is over. It will close itself once the script execution is over or the function mysql_close() is called.

Example:


<?php

// Attempt to connect to the default database server
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");

print 'You are connected';
mysql_close($link) ;

?>



See also: mysql_pconnect() and mysql_close()





Other Links

web hosting