Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

PHP MySql Error

Here is my script

$db_name = "abc_db";
$table_name = "abc_table";
$link = @mysql_connect("localhost", "user", "password") or die("Could not connect to server!");
$select_db = @mysql_select_db($db_name, $link) or die("could not select database");

I'm getting the error that "could not select database" but the databse and table actually exists and I can query them at MySql Prompt.

Please help
0
g_handa
Asked:
g_handa
  • 3
  • 2
  • 2
  • +4
1 Solution
 
phlcCommented:
check if the user you are connecting have permissions on this database.
0
 
Chad SmithSoftware EngineerCommented:
$select_db = @mysql_select_db($db_name, $link) or die("could not select database");

This line is creating the error, the function does not return a value, so you don't need to catch it.

$db_name = "abc_db";
$table_name = "abc_table";
$link = @mysql_connect("localhost", "user", "password") or die("Could not connect to server!");
mysql_select_db($db_name, $link) or die("could not select database");
0
 
phlcCommented:
retrivied from PHP Manual at http://www.php.net

mysql_select_db

(PHP 3, PHP 4 >= 4.0b1)

mysql_select_db -- Select a MySQL database

Description

bool mysql_select_db (string database_name [, resource link_identifier])

 Returns: TRUE on success, FALSE on error.
          ***************  **************

you dont need put the result in a var, but you can do this!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
g_handaAuthor Commented:
Ho do I need to set permissions
0
 
fcatakCommented:
Check your db table on mysql database.

mysql>select* from db;
you will see the user permissions for databases on MySQL.
Maybe Something wrong with the permissions.
0
 
fcatakCommented:
Check your db table on mysql database.

mysql>select* from db;
you will see the user permissions for databases on MySQL.
Maybe Something wrong with the permissions.
0
 
AJAY CHADHACommented:
close the mysql dos window and then try again. Same thing hapened with me also some days back and I solved it by closing the mysql prompt.

It happens only if you have shareware version of Mysql.

Ajay Chadha
0
 
DarinBCommented:
First, change the following:

*********************************
$link = @mysql_connect("localhost", "user", "password") or die("Could not connect to server!");
$select_db = @mysql_select_db($db_name, $link) or die("could not select database");
*********************************

TO:

**********************************
$link = mysql_connect("localhost", "user", "password");
echo mysql_error();
$select_db = mysql_select_db($db_name, $link);
echo mysql_error();
**********************************

This will echo to screen the reason that MySQL is refusing your dB select.

There are many reasons this could be happening.

darin
0
 
jimmy282Commented:
Hey Man, Two Simple Lines!
<?
mysql_connect("localhost","user","password") or die(mysql_error());
mysql_select_db($db_name);
?>

Jimmy

0
 
g_handaAuthor Commented:
Hello jimmy282

I don't know who you are but you can see the previous commnets before posting your.

g_handa
0
 
g_handaAuthor Commented:
I still have not concluded what the problem was but chadhaajay your suggestion did helped sometimes. I really don't know why..???

shubie
0
 
DarinBCommented:
Hi g handa,

You said you don't know why the error is happening.
Did you echo to the screen, the error that MySQL was sending back to PHP?? (see my previous comment)

This will tell you exactly why MySQL is refusing your database selection. You can then make a permanent fix, not one that works sometimes.

Curious why you award an "A" to a comment that works only sometimes??

darin
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now