What is use of mysql_connect in php?

Complete PHP MySQL Reference

Definition and Usage

The mysql_connect[] function opens a non-persistent MySQL connection.

This function returns the connection on success, or FALSE and an error on failure. You can hide the error output by adding an '@' in front of the function name.

Syntax

mysql_connect[server,user,pwd,newlink,clientflag]


ParameterDescription
server Optional. Specifies the server to connect to [can also include a port number. e.g. "hostname:port" or a path to a local socket for the localhost]. Default value is "localhost:3306"
user Optional. Specifies the username to log in with. Default value is the name of the user that owns the server process
pwd Optional. Specifies the password to log in with. Default is ""
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
clientflag Optional. Can be a combination of the following constants:
  • MYSQL_CLIENT_SSL - Use SSL encryption
  • MYSQL_CLIENT_COMPRESS - Use compression protocol
  • MYSQL_CLIENT_IGNORE_SPACE - Allow space after function names
  • MYSQL_CLIENT_INTERACTIVE - Allow interactive timeout seconds of inactivity before closing the connection

Tips and Notes

Tip: The connection will be closed as soon as the script ends. To close the connection before, use mysql_close[].

Tip: To establish a persistent MySQL connection, use mysql_pconnect[] instead.

Example


Complete PHP MySQL Reference

Make your web applications look like a million bucks

  

Most web applications today use boring methods to present data to their viewers using grids or simple HTML tables. FusionCharts induces "life" into the web applications by converting monotonous data into lively charts, gauges & maps.

FusionCharts works with all technologies like ASP, ASP.NET, PHP, ColdFusion, Ruby on Rails, JSP, HTML pages etc. and connects to any database to render animated & interactive charts. It takes less than 15 minutes and no expertise whatsoever to build your first chart and just a glance of it to captivate your audience. This fact is endorsed by our 12,000 customers and 150,000 users which include a majority of the Fortune 500 companies. And yeah, your applications could look like a million bucks by spending just $69.

So go ahead, download your copy of FusionCharts and start "wow-ing" your customers now!

[PHP 4, PHP 5]

mysql_connectOpen a connection to a MySQL Server

Description

mysql_connect[
    string $server = ini_get["mysql.default_host"],
    string $username = ini_get["mysql.default_user"],
    string $password = ini_get["mysql.default_password"],
    bool $new_link = false,
    int $client_flags = 0
]: resource|false

Parameters

server

The MySQL server. 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.

If the PHP directive mysql.default_host is undefined [default], then the default value is 'localhost:3306'. In SQL safe mode, this parameter is ignored and value 'localhost:3306' is always used.

username

The username. 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

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

new_link

If a second call is made to mysql_connect[] with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect[] always open a new link, even if mysql_connect[] was called before with the same parameters. In SQL safe mode, this parameter is ignored.

client_flags

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 MySQL client constants for further information. In SQL safe mode, this parameter is ignored.

Return Values

Returns a MySQL link identifier on success or false on failure.

Examples

Example #1 mysql_connect[] example

Example #2 mysql_connect[] example using hostname:port syntax

Example #3 mysql_connect[] example using ":/path/to/socket" syntax

Notes

Note:

Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket [named pipe on Windows]. If you want to use TCP/IP, use "127.0.0.1" instead of "localhost". If the MySQL client library tries to connect to the wrong local socket, the correct path should be set as mysql.default_host in php.ini and the server field left blank.

Note:

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[].

Note:

Error "Can't create TCP/IP socket [10106]" usually means that the variables_order configure directive doesn't contain character E. On Windows, if the environment is not copied the SYSTEMROOT environment variable won't be available and PHP will have problems loading Winsock.

See Also

  • mysql_pconnect[] - Open a persistent connection to a MySQL server
  • mysql_close[] - Close MySQL connection

nicodenboer at yahoo dot com

10 years ago

Be carefull here if you use utf8.

The file db.opt of your database should contain the following lines:
default-character-set=utf8
default-collation=utf8_general_ci

It means that your database is created to use the utf8 characterset.
One way to accomplish this is:
CREATE DATABASE my_database DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

Then, after connecting to it from PHP you should use:
mysql_set_charset["UTF8", $connection];

If you don't do this, you will get ugly problems in case other software is reading and writing to the same database!!!!!!

Steve

14 years ago

The too many connections issue can be due to several problems.

1. you are using pconnect. This can tie up many connections and is not really needed for MySQL as new connections are really fast.

2. Apache children are hanging around for too long - combine this with pconnect and you have recipe for disaster.

Suggestions: reduce the amount of time apache child processes stay connected to the client and how many connections before they are killed off. And don't use pconnect.

VTool

5 years ago

fcgid_module modules/mod_fcgid.so
FcgidMaxRequestLen 209715200
FcgidConnectTimeout 240
FcgidIOTimeout 240
FcgidBusyScanInterval 240
FcgidBusyTimeout 240
# Esta línea instruye al servidor web para que reconozca un tipo nuevo [php]
AddHandler fcgid-script .php
# Esta línea indica al servidor web donde está instalado PHP.
FcgidInitialEnv PHPRC "c:/php"
# Esta línea indica al servidor web que debe ejecutar la aplicación
# php-cgi.exe cuando un cliente [navegador] solicite una página con
# extensión .php
FcgidWrapper "c:/php/php-cgi.exe" .php
# Con esta línea damos los permisos necesarios para que los clientes puedan
# acceder/ejecutar a los archivos .php

Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride None
Allow from all

sky dot sama dot remove dot dots at Gmail dot com

15 years ago

In case anyone else is getting "Client does not support authentication protocol requested by server; consider upgrading MySQL client" error. The problem is the new password hashing method used by MySQL >= 4.1 mentioned below.

Either update your PHP to v5 where the new password hashing is supported or use old_password[] in MySQL 4.1.

FROM: //www.digitalpeer.com/id/mysql

UPDATE mysql.user SET password=old_password["youroldhashpassword"] WHERE user ='youruserid' and host ='yourhost'

then do

FLUSH PRIVILEGES

pepik at gmail dot cz

9 years ago

Chủ Đề