Why does it say: No database selected?

General discussions related to php

Moderators: macek, egami, gesf

acme
New php-forum User
New php-forum User
Posts: 3
Joined: Sat Jan 11, 2003 10:58 am

Why does it say: No database selected?

Postby acme » Sat Jan 11, 2003 11:04 am

Hi, i'm a newbie can someone please help me with this script?
I'm trying to get somestuff out this database but it won't work.

It keeps saying: "No database selected".
Here's the code:

Code: Select all

<?php
$server = ("Q3L");
$username = ("acme");
$password = ("");
$db = ("news");

//Maak verbinding met server
mysql_connect($server, $username);

//selecteer database
mysql_select_db($db);

//Selecteer alle id's en titels van je tabel!
$query = "SELECT id, titel FROM nieuws";

//Voer query uit!
$result = mysql_query($query) or die ("Query failed! : ".mysql_error());

//Maak OO van $result
while ($obj = mysql_fetch_object($result)){

   //Echo titel met link naar toonbericht.php
   echo '<a href="toonbericht.php?id='.$obj->id.'">'.$obj->titel.'</a><br>';
}

?>

User avatar
Joan Garnet
Moderator
Moderator
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars
Contact:

Postby Joan Garnet » Sat Jan 11, 2003 3:43 pm

Try this ->

Code: Select all

$connect = mysql_connect($server,$login,$pass);    
mysql_select_db($db,$connect);
$query = "SELECT id, ........

acme
New php-forum User
New php-forum User
Posts: 3
Joined: Sat Jan 11, 2003 10:58 am

The same

Postby acme » Sun Jan 12, 2003 2:38 am

It still sais "no database selected".

The database is there and i don't know what i did wrong :roll:

This is weird...(and i changed the code like you said)

User avatar
WiZARD
Moderator
Moderator
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol
Contact:

Re: Why does it say: No database selected?

Postby WiZARD » Sun Jan 12, 2003 4:36 am

Try change at this:

Code: Select all

<?php
$server = ("Q3L");
$username = ("acme");
$password = ("******");
$db = ("news");

//Maak verbinding met server
$connection= mysql_connect($server, $username, $pass) or die ("Sorry, connection not establishing!<BR>");

//selecteer database
mysql_select_db($db) ro die ("Sorry, database:<b>$db</b> not have exist<BR>");

//Selecteer alle id's en titels van je tabel!
$query = "SELECT id, titel FROM nieuws";

//Voer query uit!
$result = mysql_query($query, $connection) or die ("Query failed! : ".mysql_error());

//Maak OO van $result
while ($obj = mysql_fetch_object($result)){

   //Echo titel met link naar toonbericht.php
   echo '<a href="toonbericht.php?id='.$obj->id.'">'.$obj->titel.'</a><br>';
}

?>


And before to do this check you database.... maby some mistake in name of database....
And once table and database it is a diffirent....

acme
New php-forum User
New php-forum User
Posts: 3
Joined: Sat Jan 11, 2003 10:58 am

RE:

Postby acme » Sun Jan 12, 2003 8:50 am

I've tried the news code and i checked the database and i don't know why it doesnt work.

(and i made no mistakes in CAPS)
Do you have msn? Maybe you can help me there by controlling my PC ^_^. my msn address is acme22@hotmail.com

User avatar
Joan Garnet
Moderator
Moderator
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars
Contact:

Postby Joan Garnet » Sun Jan 12, 2003 1:31 pm

It should work 8O
This is the right syntax, and nothing else can be done, I mean, you connect to the server, then connect to the database and make the query.
Try this:

Code: Select all

<?php
$server="localhost";
$login="xxxxxxx";
$pass="xxxxxxx";
$db="xxxxxxx";
$table="my_table";

$user = $HTTP_POST_VARS["user"];
$passwrd = $HTTP_POST_VARS["hex_pass"];

$conection=mysql_connect($server,$login,$pass) or die("<h3>Couldn't connect to the database</h3></BODY></HTML>");     
mysql_select_db($db,$conection)or die ("<h3>Couldn't select the database</h3></BODY></HTML>");
$sql = "SELECT * FROM ".$table;
$result= mysql_query($sql,$db) or die ("<h3>Couldn't select information</h3></BODY></HTML>");
if ($row = mysql_fetch_array($result)){
   do{
      echo $row[0]."\n";
   }while   ($row = mysql_fetch_array($result));
}else{
   echo "No data found";
}
mysql_close($db);
?>


And tell us which message appears when you run the script.
bye!


Return to “PHP General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron