PHP 使用mysql 与 mysqli 连接Mysql数据库
时间:2021-07-01 10:21:17
帮助过:4人阅读
/**
* @Author: HTL
* @Email: Huangyuan413026@163.com
* @DateTime: 2015-05-14 16:00:08
* @Description: Description
*/
// 降低PHP默认的错误级别
// 只显示除禁用以外的所有错误
// 解决因为PHP5.3+版本太高而导致在使用mysql_connect时出现的弃用警告“Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead”
error_reporting(E_ALL ^
E_DEPRECATED);
$db_host=
"localhost";
$db_user=
"root";
$db_passwd=
"";
$db_name=
"mysql";
echo("<BR>---------------------------mysql_connect------------------------<BR><BR>");
$query =
"SELECT SESSION_USER(), CURRENT_USER(),now();";
$conn =
mysql_connect($db_host, $db_user,$db_passwd);
if (!
$conn)
{
die(‘Could not connect: ‘ . mysql_error());
}
mysql_select_db($db_name, $conn);
$result =
mysql_query($query);
while($row =
mysql_fetch_array($result)){
var_dump($row);
}
mysql_close($conn);
echo("<BR>---------------------------mysqli_connect------------------------<BR><BR>");
$conn =
mysqli_connect($db_host, $db_user, $db_passwd,$db_name) ;
if (!
$conn)
{
die(‘Could not connect: ‘ . mysqli_error());
}
//execute the query.
$result =
mysqli_query($conn, $query);
//display information:
while($row =
mysqli_fetch_array($result)) {
var_dump($row);
}
mysqli_close($conn);
exit();
?>
有图有真相
参考:
解决Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in
w3school PHP MySQL Select
PHP 手册 error_reporting
来自为知笔记(Wiz)
PHP 使用mysql 与 mysqli 连接Mysql数据库
标签: