“No database selected” error even after a db is se

2020-04-07 17:33发布

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.

I have never seen this error before and i tried to change a lot of things but its just not working.

标签: php mysql
6条回答
够拽才男人
2楼-- · 2020-04-07 18:13

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.

查看更多
太酷不给撩
3楼-- · 2020-04-07 18:14

May be mysql's user permissions on the database you are selecting?

查看更多
够拽才男人
4楼-- · 2020-04-07 18:16

Check if you have added the user to the db and gave him appropriate permissions.

查看更多
Anthone
5楼-- · 2020-04-07 18:27

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.

查看更多
地球回转人心会变
6楼-- · 2020-04-07 18:30

You forgot to pass the variable $link as the link parameter.

     mysql_select_db('database', $link) or die(mysql_error());

EDIT: Try passing the database in the FROM parameter like

     SELECT * FROM `database`.`table`
查看更多
贼婆χ
7楼-- · 2020-04-07 18:35

SOLVED Use ` backticks for MYSQL reserved words... your table name is reserved word for MYSQL... Change your table name.

查看更多
登录 后发表回答