尝试使用 PHP 连接到数据库时遇到问题。我收到以下错误
Notice: Undefined variable: dbhandle in /opt/lampp/htdocs/connection/Connection.php on line 17
Warning: mysql_select_db() expects parameter 2 to be resource, null given in /opt/lampp/htdocs/connection/Connection.php on line 17
Could not select test
我的连接文件:
<?php
function Connection() {
$username = "root";
$password = "";
$hostname = "localhost";
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
mysql_query("SET NAMES utf8");
}
function SelectDatabase() {
$name = "test";
$selected = mysql_select_db("$name",$dbhandle)
or die("Could not select $name");
}
?>
索引.php
<html>
<head>
<?php include 'Connection.php'; ?>
</head>
<body>
<?php Connection() ?>
<?php SelectDatabase() ?>
</body>
</html>