How to Select a MySQL Database

Before you get your code up and running, you have to do a few things. One of the things that you will have to do is select the MySQL database.

The reason that you have to select the database is that the script has to know which database to use. Within the whole MySQL server, there can be as many databases as resources allow. The database that you are using has to be specified, other your queries will not run.

The syntax to select the MySQL database is as follows:

{code type=php}mysql_select_db ($database_name , [$link_identifier]){/code}

The first parameter is fairly self explanatory. The second one is the link identifier returned by the mysql_connect function that you had to have used before calling this function. You don’t have to pass this second parameter – the last created link is used automatically if this is not included.

The function returns true if the link was successfully created, otherwise false.

It’s also possible to work around this if you need to make queries to multiple databases in a short amount of time. You can specify the database that a table resides under in the query itself, like so:

{code type=sql}SELECT * FROM database_name.table_name{/code}

It’s much easier to just select the MySQL database if you are using the same database every time, though. Especially if it’s possible that the name of the database would change, since that would force you to change the name in all of the places that it appeared directly in queries.

If you have any questions about selecting databases, please leave a comment!


Leave a Reply

Your email address will not be published.

More Posts

How to use PHP Type Declarations

PHP has developed into a more strictly typed language in recent years. Type Declarations help with that and are a fairly new addition to PHP,


If you have come to JavaScript from a background in Java or C, then you will like the variation on the for loop. It is

Context Object Manipulation

One of the key concepts of JavaScript is the keyword this, which signifies the JavaScript context object. If you write an inner function, you can access

Polymorphic Functions

It is possible to define multiple functions with the same name in JavaScript, and the last one of those that you define will be the