1.最简单的方式
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_close($con);
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_close($con);
3.
pdo连接mysql
(详细教程)
$db = new PDO('mysql:host=localhost;dbname=test', 'root', '');
try {
foreach ($db->query('select * from user') as $row){
print_r($row);
}
$db = null; //关闭数据库
} catch (PDOException $e) {
echo $e->getMessage();
}
?>
4.
ADODB连接mysql
(详细教程)
require_once './adodb5/adodb.inc.php';
$conn = &ADONewConnection('mysql');
$conn->connect('localhost','root','','test');
$conn->Execute("set names utf8");
$res = $conn->Execute("select * from user");
if (!$res){
echo $conn->ErrorMsg();
}else{
var_dump($res);
}
?>