Click here to Skip to main content
13,049,172 members (66,465 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I had got an error as "Can't use db : No database selected".I got this error at the time using the following code....



// Start XML file, create parent node
$doc = domxml_new_doc("1.0");
$node = $doc->create_element("markers");
$parnode = $doc->append_child($node);

// Opens a connection to a MySQL server
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());

// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());

// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());

header("Content-type: text/xml");

// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
$node = $doc->create_element("marker");
$newnode = $parnode->append_child($node);

$newnode->set_attribute("name", $row['name']);
$newnode->set_attribute("address", $row['address']);
$newnode->set_attribute("lat", $row['lat']);
$newnode->set_attribute("lng", $row['lng']);
$newnode->set_attribute("type", $row['type']);

$xmlfile = $doc->dump_mem();
echo $xmlfile;

Posted 13-Sep-12 17:20pm
Peter_in_2780 13-Sep-12 23:39pm
Looks pretty obvious to me. The error message tells you that the line
$db_selected = mysql_select_db($database, $connection);
is returning false. The connection is OK (otherwise the previous block would have died) so the only thing left is that the database named in $database just doesn't exist.
ahmed559 13-Sep-12 23:44pm
$database="username-databaseName"; Is it database name.

I used in this way:

// Set the active mySQL database
$mysql_select_db = mysql_select_db($DB_Name, $connection);
if (!$mysql_select_db) {
die ('Can\'t use db : ' . mysql_error());
Peter_in_2780 13-Sep-12 23:56pm
Sit down.
Take a deep breath.
Read my previous comment slowly.
Grok it.
ahmed559 14-Sep-12 0:19am
Can you give me a detailed answer once again ?

Peter_in_2780 14-Sep-12 0:42am
As I said above, the database named in $database just doesn't exist. Hint: include $database in the die message, then you can see what you're trying to do.
if (!$mysql_select_db) {
die ('Can\'t use db : '. $database . ' because' . mysql_error());
Mohibur Rashid 13-Sep-12 23:51pm
Either your so called user dont have the permission to access your database or you think this is your database name but it is not. check your database name properly. and if it is not windows then also check for case of the database name
ahmed559 14-Sep-12 0:11am
I had try in this way:

$connection = mysql_connect($config['DB_Host'], $config['DB_User'],$config['DB_Passwd']);
if (!$connection) {
die('Not connected : ' . mysql_error());

databse connection created in config.php.All other data getting from database.But problem arises when trying to make google map from eisting database.
Kislay Raj 15-Sep-12 1:58am
In Permission case Error Message will differ
Kislay Raj 15-Sep-12 1:50am
ya error is in following block
$db_selected = mysql_select_db($db, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
i had Test your Code it's work fine I think you should check the database name. may be miss spell
ahmed559 15-Sep-12 2:02am
already fixed!!!
Kislay Raj 15-Sep-12 2:03am
may you tell me please? what was the error?
ahmed559 15-Sep-12 2:05am
database connection created in this way:

or die("Cannot Caonnect to database");
mysql_select_db($databasename,$connect) or die
("Cannot find database");

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 13 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100