php 即使选择了数据库,也会出现“未选择数据库”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7988734/
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
"No database selected" error even after a db is selected
提问by Ricardo
I have selected the database but for some weird reason it still says that it is not selected.
我已经选择了数据库,但出于某种奇怪的原因,它仍然说它没有被选中。
Connection lines:
连接线:
$location = "localhost";
$user = "user";
$pass = "pass";
//Database Selection
$link = mysql_connect($location, $user, $pass);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
//Database Selection
mysql_select_db('database') or die(mysql_error());
The Query:
查询:
while ($row_fpages = mysql_fetch_array($result_fanpage))
{
if ( $row_fpages['client'] == NULL ) {
//GRAPHS
$sql = "SELECT date, likes FROM statistics_pages WHERE idnum='".$_COOKIE['id']."' AND page_name = ".$row_fpages['page_name']." LIMIT 7";
$result_apps = mysql_query($sql) or die(mysql_error());
And the error is a plain No database selected
.
错误是一个简单的No database selected
.
I have never seen this error before and i tried to change a lot of things but its just not working.
我以前从未见过这个错误,我试图改变很多东西,但它只是不起作用。
回答by David Bélanger
You forgot to pass the variable $link as the link parameter.
您忘记将变量 $link 作为链接参数传递。
mysql_select_db('database', $link) or die(mysql_error());
EDIT:Try passing the database in the FROM parameter like
编辑:尝试在 FROM 参数中传递数据库,如
SELECT * FROM `database`.`table`
回答by Vykthur D .
Another reason why this error pops up is because the username and password which you are using to access the database may not have appropriate read/write persmissions. Especially on a shared hosting server, you will need to assign the appropriate username to the corresponding database. I recently had this problem, I had created a new database and forgot to assign persmissions for my existing username.
出现此错误的另一个原因是您用来访问数据库的用户名和密码可能没有适当的读/写权限。特别是在共享托管服务器上,您需要为相应的数据库分配适当的用户名。我最近遇到了这个问题,我创建了一个新数据库,但忘记为我现有的用户名分配权限。
回答by Diego
May be mysql's user permissions on the database you are selecting?
可能是您选择的数据库的 mysql 用户权限?
回答by CleanX
Check if you have added the user to the db and gave him appropriate permissions.
检查您是否已将用户添加到数据库并授予他适当的权限。
回答by Nazim Ahmed
SOLVED Use ` backticks for MYSQL reserved words... your table name is reserved word for MYSQL... Change your table name.
已解决 对 MYSQL 保留字使用 ` 反引号...您的表名是 MYSQL 的保留字...更改您的表名。
回答by Marc DiMillo
Make sure your code isn't inserted into classes and stuff, if it's just presented as is, then perhaps 'database' isn't valid, double check your values and try making it an external variable.
确保您的代码没有插入到类和东西中,如果它只是按原样呈现,那么可能“数据库”无效,请仔细检查您的值并尝试将其设为外部变量。