Oracle 5.0 Reference Manual page 2268

Table of Contents

Advertisement

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
Example 20.28.
mysql_connect
<?php
// we connect to example.com and port 3307
$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
// we connect to localhost at port 3307
$link = mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
Example 20.29.
mysql_connect
<?php
// we connect to localhost and socket e.g. /tmp/mysql.sock
// variant 1: omit localhost
$link = mysql_connect(':/tmp/mysql', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
// variant 2: with localhost
$link = mysql_connect('localhost:/tmp/mysql.sock', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
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, you
should set the correct path as
configuration and leave the server field blank.
Original MySQL API (Mysql)
example using
hostname:port
example using ":/path/to/socket" syntax
mysql.default_host
2248
syntax
string
in your PHP

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents