MySQL Unknown Database 'Database Name' Error

nostradamus

New Member
Messages
10
Reaction score
0
Points
1
Hi, all. I'm currently learning MySQL by a tutorial on tizag.com, and I've seem to hit an error. I'm told to access one of my databases by using the following code:

<?php

mysql_select_db("MyDatabaseName") or die(mysql_error());

echo "Connected to database.";

?>


I am connected to the server of localhost, and I am providing the right database name, so I'm a bit confused to what I'm doing wrong. I'm still a bit disoriented in the whole localhost area and connecting to databases remotely, so maybe I'm doing something wrong, ha.

Thanks.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Are you sure you are connected? Is this with x10hosting's mySQL server? What exactly is the error message?
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Ignore that tutorial as far as bridging PHP and MySQL is concerned, as the mysql drive is outdated and has been supplanted twice over. Switch to using PDO. Try "Writing MySQL Scripts with PHP and PDO" for a tutorial.
 
Last edited:

bdistler

Well-Known Member
Prime Account
Messages
3,534
Reaction score
196
Points
63
The script is not a "tutorial" but a tester that will run on most versions
of MySQL and from about versions 4.5 of PHP all the way up to the current version.

This 'Thread' is named "MySQL Unknown Database 'Database Name' Error"

nostradamus said:
Hi, all. I'm currently learning MySQL by a tutorial on tizag.com, and I've seem to hit an error.

The script is in the same 'style' of that use at www.tizag.com

misson said:
Ignore that tutorial...

I do not know where 'misson' received the authority to command us to "ignore" anything
much more "ignore" the world we study and work in.

Introduction to PHP - PDO

From above link

Introduction

The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. Each database driver that implements the PDO interface can expose database-specific features as regular extension functions. Note that you cannot perform any database functions using the PDO extension by itself; you must use a database-specific PDO driver to access a database server.

PDO provides a data-access abstraction layer, which means that, regardless of which database you're using, you use the same functions to issue queries and fetch data. PDO does not provide a database abstraction; it doesn't rewrite SQL or emulate missing features. You should use a full-blown abstraction layer if you need that facility.

PDO ships with PHP 5.1, and is available as a PECL extension for PHP 5.0; PDO requires the new OO features in the core of PHP 5, and so will not run with earlier versions of PHP.

When you are testing for why your PHP/MySQL code is not working
better to test without PDO and be sure it is your code and not
how PHP/MySQL, on your system, is implemented
 
Last edited:

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
I'm going to have to backup misson here: there is no 'authority', but using the most recent and up to date driver is just plain common sense. If you don't feel like using PDO, then use mysqli as a bare minimum and just completely avoid the old mysql functions. Drivers become outdated for a reason, in this case you would be sacrificing the security, simplicity and speed benefits you could get through using the PDO driver.
So essentially, any script that is still using the twice outdated mysql driver is a pretty massive signal that the developer has either given up on it or does not have the necessary skill to upgrade it. Even for a simple database testing script, using PDO is good practice and just as simple as trying to use. PDO is enabled on all the free servers, so there is no excuse not to use it.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
The script is not a "tutorial" but a tester that will run on most versions
of MySQL and from about versions 4.5 of PHP all the way up to the current version.
What makes you sure? The OP stated the script is a part of a tutorial. Moreover, if you head over to tizag and look at their mysql connect tutorial, you see a script remarkably similar to what the OP posted (the lack of a call to mysql_connect in the posted script likely being a mistake on the OP's part, one that is causing the error). The script in the tutorial isn't to test connecting to MySQL; it illustrates how to create a connection and is then built upon to show how to perform other tasks.

[...]I do not know where 'misson' received the authority to command us to "ignore" anything
much more "ignore" the world we study and work in.
Experience gives me the ability to offer advice. You're free to disagree, but that doesn't justify copping an attitude. The snarky tone is just uncivil.

The OP is attempting to learn how to access MySQL from PHP. Any tutorial that is based on the old mysql driver is out of date, almost certainly doesn't teach best practices and probably does teach bad habits (such as using "or die(mysql_error())" for error handling without qualification). The use of the old mysql driver is basically a tutorial smell. Once he's learned the modern way of interfacing with MySQL in PHP, OP can learn outdated interfaces, if he wants.

By the way, are those air quotes around my handle?

Introduction to PHP - PDO

From above link

[...]PDO ships with PHP 5.1, and is available as a PECL extension for PHP 5.0; PDO requires the new OO features in the core of PHP 5, and so will not run with earlier versions of PHP.
PHP 4 is past the end of its lifetime. Installing PHP 5 is a trivial task for an admin; any host that doesn't offer PHP5 is either lazy or doesn't know what they're doing. PHP 4 can be left installed for legacy code, but new code should be based on PHP 5. This is the position of the PHP developers, who stopped supporting PHP 4 in 2008.

When you are testing for why your PHP/MySQL code is not working
better to test without PDO and be sure it is your code and not
how PHP/MySQL, on your system, is implemented
OP is learning, not testing.

PDO isn't an implementation detail. If PDO is an option, there's no reason to use the old mysql driver; new PDO("mysql:host=$host", $user, $pw) will work or fail as mysql_connect($host, $user, $pw);. If PDO isn't an option, mysqli is a better choice than mysql. A better choice still is to find a modern host.
 
Messages
1
Reaction score
0
Points
0
MY CODE HAS BEEN PRETTY WORKING ON LOCAL HOST..

<?php
$username = "root";
$password = "";
$database = "db_name";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Unable to select database");
?>


it is my practice not to display message when connected so when i type the phpfile name of my conenctivity it show nothing but when error occurs it show "Unable to select database" this is very easy to set up. you can also use a modified username for your mysql database just to to operations and add users.. cause "root" is a very big security hole.. not just if your at home studying using local host..
 
Top