我在尝试使用PHP连接到数据库时遇到问题。我收到以下错误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");
}
?>
的index.php
<html>
<head>
<?php include 'Connection.php'; ?>
</head>
<body>
<?php Connection() ?>
<?php SelectDatabase() ?>
</body>
</html>
为什么你仍然在使用outdate API,为什么要将数据库代码与HTML代码混合? – ThiefMaster
什么是最新的API? – Colin747
@ Colin747 - PDO或mysqli。请参阅http://www.php。net/manual/en/mysqlinfo.api.choosing.php – j08691