How to choose a database using SELECT DATABASE in MariaDB
Once you have connected to the open-source database management system, you need to select the database you want to work with in MariaDB. You have two options for this: you can use the USE command in the MySQL command line or the mysql_select_db function in PHP. We will cover both methods.
The command USE in the command line
The syntax of USE is as follows:
USE name_of_database;sqlYou must always use the command in combination with a special database and use this instead of the placeholder ‘name_of_database’. If you omit this parameter, you will receive an error message (ERROR 1046).
To make it easier for you to understand how this works, we will use a simple example. Let’s imagine that we want to access the ‘Customers’ database. The following steps are necessary:
- Log in to your server via the command line:
mysql -u root -p
Enter password: ************sql- Use the
SHOW DATABASEScommand to get an overview of all available databases on your server:
mysql> SHOW DATABASES;sql- To select the desired database, use the command
USE:
mysql> USE customers;sqlNow you can work in the database and create a new table with MariaDB CREATE TABLE. If the desired database is not yet listed, create it with the MariaDB command CREATE DATABASE. If a database is no longer required, remove it with the MariaDB command DROP DATABASE.
SELECT DATABASE for MariaDB in PHP
The function SELECT DATABASE for MariaDB can also be found in PHP (here: mysqli_select_db). The syntax for establishing the connection is as follows:
$connection = mysqli_connect("server", "username", "password");phpTo select the database, the subsequent command looks like this:
mysqli_select_db($connection, "customer");php