【发布时间】:2012-09-30 22:39:23
【问题描述】:
我在尝试使用 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>
【问题讨论】:
-
为什么还在使用过时的 API 以及为什么将数据库代码与 HTML 代码混合使用?
-
最新的 API 是什么?
-
@Colin747 - PDO 或 mysqli。见php.net/manual/en/mysqlinfo.api.choosing.php
-
"$var"是货物崇拜编程的标志。
标签: php connection database-connection