Click here to Skip to main content
15,886,798 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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....

$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
Comments
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.
Relax.
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 ?

Kindly...
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());
}

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900