mysql_connect
mysql_connect
Open a connection to a MySQL Server
Waarschuwing: >
mysqli_connect``PDO::__construct
**mysql_connect** string $server string $username string $password bool $new_link int $client_flags
Opens or reuses a connection to a MySQL server.
serverThe 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 is undefined (default), then the default
value is 'localhost:3306'. In sqlsafemode, this parameter is ignored
and value 'localhost:3306' is always used.
mysql.default_host
username
The username. Default value is defined by . In
sqlsafemode, this parameter is ignored and the name of the user that
owns the server process is used.
mysql.default_user
password
The password. Default value is defined by . In
sqlsafemode, this parameter is ignored and empty password is used.
mysql.default_password
new_link
If a second call is made to
with the same arguments, no new link will be established, but
instead, the link identifier of the already opened link will be
returned. The parameter modifies this
behavior and makes always open
a new link, even if was called
before with the same parameters.
In sqlsafemode, this parameter is ignored.
mysql_connect``new_link``mysql_connect``mysql_connect
client_flags
The parameter can be a combination
of the following constants:
128 (enable handling),
,
,
or
.
Read the section about for further information.
In sqlsafemode, this parameter is ignored.
client_flags``LOAD DATA LOCAL``MYSQL_CLIENT_SSL``MYSQL_CLIENT_COMPRESS``MYSQL_CLIENT_IGNORE_SPACE``MYSQL_CLIENT_INTERACTIVE
Returns a MySQL link identifier on successreturn.falseforfailure.
Voorbeeld: example
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
Voorbeeld: example using syntax
<?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);
?>
Voorbeeld: example using ":/path/to/socket" syntax
<?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);
?>
Opmerking: > 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 in php.ini and the server field left blank. mysql.default_host
Opmerking: > 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
Opmerking: > Error "Can't create TCP/IP socket (10106)" usually means that the configure directive doesn't contain character . On Windows, if the environment is not copied the environment variable won't be available and PHP will have problems loading Winsock. variables_order
E``SYSTEMROOT
mysql_pconnect``mysql_close