mysql_connect

mysql_connect — Open a connection to a MySQL Server

Version: (PHP 4, PHP 5)

Syntax: mysql_connect($server,$user,$password,$newlink,$clientflag)

Description: The mysql_connect() function opens a non-persistent MySQL connection.

server      The server is a String. It can also include a port number. e.g. "hostname:port" or a path to a local socket e.g. ":/path/to/socket" for the localhost.

user   User is a String containing the user name. Default value is defined by mysql.default_user. In SQL safe mode, this parameter is ignored and the name of the user that owns the server process is used.

password     password is a String containing the password. Default value is defined by mysql.default_password. In SQL safe mode, this parameter is ignored and empty password is used.

newlink    Optional. If a second call is made to mysql_connect() with the same arguments, no new connection will be established; instead, the identifier of the already opened connection will be returned.(Version: 4.2.0)

clientflag    The client_flags  parameter can be a combination of the following constants:

   128 (enable LOAD DATA LOCAL handling), MYSQL_CLIENT_SSL, MYSQL_CLIENT_COMPRESS, MYSQL_CLIENT_IGNORE_SPACE or MYSQL_CLIENT_INTERACTIVE. Read the section about Predefined Constants for further information. In SQL safe mode, this parameter is ignored.

Return Values: This function returns the connection on success, or FALSE and an error on failure.

# Example: mysql_connect()

<?php
$con = mysql_connect("localhost", "user", "password");
if (!$con) {
    die("Could not connect:" . mysql_error());
}
echo 'Connected successfully';

mysql_close($con); ?>

<?
    function mysql_connect($localhost = NULL, $user = NULL, $password = NULL, $new = false, $flags = 0) {
        global $__mysql;

        $extra_info = substr(strstr($localhost, ":"), 1);         if(!empty($extra_info)) {             if(is_numeric($extra_info)) {                 $port = $extra_info;             } else {                 $socket = $extra_info;             }             $localhost = substr($localhost, 0, strlen($localhost)-(strlen($extra_info)+1));         }

        if(empty($socket)) {             if(empty($port)) {              $__mysql = mysqli_connect($localhost, $user, $password);             } else {              $__mysql = mysqli_connect($localhost, $user, $password, NULL, $port);             }         } else {             $__mysql = mysqli_connect($localhost, $user, $password, NULL, NULL, $socket);         }         return $__mysql;     } ?>

Post Comment
Login to post comments