Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PHP MySQL
I had got an error as "Can't use db : No database selected".I got this error at the time using the following code....
 
$username="username";
$password="password";
$database="username-databaseName";
?>
 

require("phpsqlajax_dbinfo.php");
 
// 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)){
// ADD TO XML DOCUMENT NODE
$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 18:20pm
Comments
Peter_in_2780 at 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 at 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 at 13-Sep-12 23:56pm
   
Sit down.
Relax.
Take a deep breath.
Read my previous comment slowly.
Grok it.
ahmed559 at 14-Sep-12 0:19am
   
Can you give me a detailed answer once again ?
 
Kindly...
Peter_in_2780 at 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 at 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 at 14-Sep-12 0:11am
   
I had try in this way:
 
$connection = mysql_connect($config['DB_Host'], $config['DB_User'],$config['DB_Passwd']);
mysql_select_db($config['DB_Name'],$connection);
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.
 
https://developers.google.com/maps/articles/phpsqlajax_v3#createmap
Kislay Raj at 15-Sep-12 1:58am
   
In Permission case Error Message will differ
Kislay Raj at 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 at 15-Sep-12 2:02am
   
already fixed!!!
Kislay Raj at 15-Sep-12 2:03am
   
may you tell me please? what was the error?
ahmed559 at 15-Sep-12 2:05am
   
database connection created in this way:
 
$host="localhost";
$username="root";
$password="";
$databasename="";
$connect=mysql_connect($host,$username,$password)
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
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,260


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 13 Sep 2012
Copyright © CodeProject, 1999-2014
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