Solved

PHP MySql Error

Posted on 2001-08-14
12
225 Views
Last Modified: 2006-11-17
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
Comment
Question by:g_handa
  • 3
  • 2
  • 2
  • +4
12 Comments
 

Expert Comment

by:phlc
ID: 6385622
check if the user you are connecting have permissions on this database.
0
 
LVL 10

Expert Comment

by:Chad Smith
ID: 6385752
$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
 

Expert Comment

by:phlc
ID: 6385787
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
 

Author Comment

by:g_handa
ID: 6387079
Ho do I need to set permissions
0
 

Expert Comment

by:fcatak
ID: 6387228
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
 

Expert Comment

by:fcatak
ID: 6387310
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Accepted Solution

by:
AJAY CHADHA earned 100 total points
ID: 6454055
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
 
LVL 3

Expert Comment

by:DarinB
ID: 6462465
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
 
LVL 11

Expert Comment

by:jimmy282
ID: 6501925
Hey Man, Two Simple Lines!
<?
mysql_connect("localhost","user","password") or die(mysql_error());
mysql_select_db($db_name);
?>

Jimmy

0
 

Author Comment

by:g_handa
ID: 6501974
Hello jimmy282

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

g_handa
0
 

Author Comment

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

shubie
0
 
LVL 3

Expert Comment

by:DarinB
ID: 6506209
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP7 and Sql Server Windows 2008 R2 13 85
Amazon Redshift 2 25
php string masking 4 27
Configuring a checkbox in CSS and php 18 30
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now