【发布时间】:2016-02-22 03:51:55
【问题描述】:
我正在我的电脑上的 LAMP 下测试本地登录数据库页面。即使我可以从终端连接到 mysql,我也不能从 php 脚本中这样做。我已经在网上到处查看了,但无论我走到哪里,它都只是以下代码的一种或另一种变体
<!DOCTYPE HTML5>
<html>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css" href="style.css">
<head>
</head>
<body>
<?php
$username = $_POST['username'];
$password = $_POST['password'];
/* connect to the db */
$connection = mysql_connect('localhost', 'username', 'password') or die ("Connect error");
mysql_select_db('myDatabase',$connection);
/* show tables */
$res = mysql_query("SHOW DATABASES", $connection) or die ("No Show Database");
while ($row = mysql_fetch_row($res))
{
echo $row[0], '<br/>';
}
?>
</body>
</html>
还有另一个页面需要用户名和密码,然后通过 POST 方法将其传递到此页面,但此页面立即显示连接错误。我尝试使用 if else 而不是 or die 但仍然无法连接。
【问题讨论】:
-
or die ("Connect error");对您没有帮助。or die(mysql_error())将,or die ("No Show Database");相同 -
如果可以的话,你应该stop using
mysql_*functions。 These extensions 已在 PHP 7 中删除。了解PDO 和 MySQLi 的 prepared 语句并考虑使用 PDO,it's really not hard。 -
这里的目标是什么?使用 POST 数组/变量并充当数据库连接的参数?