Click to See Complete Forum and Search --> : Can't connect to db


MadamZuZu
04-27-2007, 01:11 PM
hi :)
im VERY new to PHP.

i'm tyring to connect to a db. and it's just not working :(

please help.

this is what i did to create the database:


CREATE TABLE Quotes (
ID int(11) NOT NULL auto_increment,
Quote char(255) default NULL,
Name char(255) default NULL,
IP char(255) default NULL,
Date char(255) default NULL,
PRIMARY KEY (ID)
) TYPE=MyISAM;



this is what i'm using to connect to it:

$dbhost = "myhost";
$dbusername = "myusername";
$dbuserpass = "mypassword";
$default_dbname = "quotes";

$connection = mysql_connect($dbhost, $dbusername, $dbuserpass);
if (!$connection)
{
die('Could not connectooo: ' . mysql_error());
}
print "++Connected to MySQL<br>";

print"default: $default_dbname";
@mysql_select_db($default_dbname) or die( "Unable to select database");


and i keep getting this as the output:
++Connected to MySQL
default: quotesUnable to select database

pleeeaase help

susanta
04-27-2007, 02:57 PM
The problem isnt with PHP, its in Mysql.

You have used 'CREATE TABLE...' command which creates a table in the default databse and NOT a database. Therefore what you have create is a table not a database and PHP cant obviously find a database of that name.

Either use 'CREATE DATABASE Qoutes' inside of which you'll ve to create a table 'qoutes', if you like, by using the same commad that you ve used i.e. 'CREATE TABLE...'

Or you can loggin to your MySql database and use 'SELECT DATABASE();' command to check your default database name.

I am not myself a expert in PHP. If you have any further problems, feel free to ask.

regards.

susanta
04-27-2007, 03:09 PM
You have created a table in MySql with the name 'Qoutes' using the "CREATE TABLE Quotes" command, and not a database. It is possibly created in your default database, the name of which is not 'Quotes'. Therefore PHP is unable to select the database named quotes.

You can either create another database named 'quotes' using "CREATE DATABASE Quotes;" command in your mysql. And then create this table inside of that database.

Or else you can just loggin to MySql and check for the name of current database using "SELECT DATABASE();" command. replace "quotes in
$defaulf_dbname = "quotes"
with what is displayed by MySql

Regards

aj_nsc
04-27-2007, 03:10 PM
Could it be because you named your database 'Quotes' and you are trying to connect to 'quotes'?

EDIT: I posted at the same time as the previous post and also, wow, yeah that's right, how 'd I miss that?

MadamZuZu
04-27-2007, 11:56 PM
i created a database in my hosting admin. so it's there

connection = mysql_connect($dbhost, $dbusername, $dbuserpass);
connects just fine.
if i put an if on the "conntectioN" it returns a positive result
"successfull connection"

maaaan. i'm confused ;(

NogDog
04-28-2007, 01:57 AM
You are not failing on the connection, but on the database selection. It might help to do a mysql_error() if it fails, just like you did for the mysql_connect:

@mysql_select_db($default_dbname) or die( "Unable to select database: " . mysql_error());