首页 > 代码库 > 测试php与mysql的连接是否成功的多种方法
测试php与mysql的连接是否成功的多种方法
测试php与mysql的连接是否成功的多种方法其实使用的就是一个php的函数:
mysql_connect();
定义
mysql_connect() 函数打开非持久的 MySQL 连接。
用法:
<?php
$link = mysql_connect(‘localhost‘, ‘mysql_user‘, ‘mysql_password‘);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
echo ‘Connected successfully‘;
mysql_close($link);
?><?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);
?><?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);
?>
本文出自 “随记” 博客,请务必保留此出处http://xiaozuo2010925.blog.51cto.com/3528655/1886282
测试php与mysql的连接是否成功的多种方法