未选择数据库 - PHP & MySQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8513484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:53:32  来源:igfitidea点击:

No Database Selected - PHP & MySQL

phpmysql

提问by Joe Torraca

I am trying to connect to my database and when I run the code, I get an error. Can anybody tell me what is wrong also any errors in my PHP code? Thanks.

我正在尝试连接到我的数据库,当我运行代码时,出现错误。任何人都可以告诉我有什么问题,我的 PHP 代码中有什么错误吗?谢谢。

Error: No database selected

PHP Code:

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);

.conf.phpfile:

.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);
?>

回答by Michael Berkowski

Unless you have the password incorrect and need to fix it, run a GRANTstatement to grant access to your database user:

除非您的密码不正确并需要修复它,否则请运行一条GRANT语句以授予对您的数据库用户的访问权限:

GRANT ALL PRIVILEGES ON aTable.* TO xxx@localhost IDENTIFIED BY 'password_for_xxx';

The above grants all privileges. It's often better to restrict to only what's needed. For example, if you only intend to SELECT, but not modify data,

以上授予所有特权。通常最好只限制在需要的范围内。例如,如果你只打算 SELECT,而不是修改数据,

GRANT SELECT ON aTable.* TO xxx@localhost IDENTIFIED BY 'password_for_xxx';

Update

更新

Since you have identified the database name as dedbmysql, change your mysql_select_db()call to:

由于您已将数据库名称标识为dedbmysql,请将您的mysql_select_db()调用更改为:

$db = mysql_select_db("dedbmysql", $con);

Following this, the GRANTstatements above are probably unnecessary, as your host may have already worked out the correct database permissions.

在此之后,GRANT上面的语句可能是不必要的,因为您的主机可能已经计算出正确的数据库权限。

回答by h4ck3rm1k3

I had that problem and solved it with prefixing the table name with the database name, so

我遇到了这个问题并通过在表名前加上数据库名来解决它,所以

 select * from database.table