我正在尝试连接到我的数据库,当我运行代码时,我收到一个错误.任何人都能告诉我PHP代码中的错误是什么问题吗?谢谢.
Error: No database selected
Run Code Online (Sandbox Code Playgroud)
PHP代码:
include('.conf.php');
$prof = $_GET['profile'];
$prof = addslashes(htmlentities($prof));
$query = "SELECT * FROM aTable where id = '".mysql_real_escape_string($prof)."'";
$info_array = mysql_query($query, $con) or die (mysql_error()).;
while($row = mysql_fetch_array( $info_array ))
{
echo $row['num1'];
echo "</br>";
echo $row['num2'];
echo "</br>";
echo $row['num3'];
echo "</br>";
echo $row['num4'];
};
mysql_close($con);
Run Code Online (Sandbox Code Playgroud)
.conf.php文件:
<?php
$conf['db_hostname'] = "xxx";
$conf['db_username'] = "xxx";
$conf['db_password'] = "xxx";
$conf['db_name'] = "xxx";
$conf['db_type'] = "mysql";
$con = mysql_connect('xxx', 'xxx', 'xxx') or die (mysql_error());
$db = mysql_select_db("aTable", $con);
?>
Run Code Online (Sandbox Code Playgroud)
h4c*_*1k3 15
我遇到了这个问题并通过在表名前加上数据库名称来解决它,所以
select * from database.table
Run Code Online (Sandbox Code Playgroud)
Mic*_*ski 14
除非您的密码不正确且需要修复密码,否则请运行GRANT语句以授予对数据库用户的访问权限:
GRANT ALL PRIVILEGES ON aTable.* TO xxx@localhost IDENTIFIED BY 'password_for_xxx';
Run Code Online (Sandbox Code Playgroud)
以上授予所有特权.通常最好只限制所需的内容.例如,如果您只打算SELECT,而不是修改数据,
GRANT SELECT ON aTable.* TO xxx@localhost IDENTIFIED BY 'password_for_xxx';
Run Code Online (Sandbox Code Playgroud)
由于您已将数据库名称标识为dedbmysql,因此将您的mysql_select_db()呼叫更改为:
$db = mysql_select_db("dedbmysql", $con);
Run Code Online (Sandbox Code Playgroud)
在此之后,上述GRANT语句可能是不必要的,因为您的主机可能已经计算出正确的数据库权限.