Once you get connected with the MySQL server, it is required to select a database to work with. This is because there might be more than one database available with the MySQL Server.
Selecting MySQL Database from the Command Prompt
It is very simple to select a database from the mysql> prompt. You can use the SQL command use to select a database.
Example
Here is an example to select a database called TUTORIALS −
[root@host]# mysql -u root -p Enter password:****** mysql> use TUTORIALS; Database changed mysql>Now, you have selected the TUTORIALS database and all the subsequent operations will be performed on the TUTORIALS database.
NOTE − All the database names, table names, table fields name are case sensitive. So you would have to use the proper names while giving any SQL command.
Selecting a MySQL Database Using PHP Script
PHP uses mysqli_select_db function to select the database on which queries are to be performed. This function takes two parameters and returns TRUE on success or FALSE on failure.
Syntax
mysqli_select_db ( mysqli $link , string $dbname ) : bool1 | $link Required - A link identifier returned by mysqli_connect() or mysqli_init(). |
2 | $dbname Required - Name of the database to be connected. |
Example
Try the following example to select a database −
Copy and paste the following example as mysql_example.php −
<html> <head> <title>Selecting MySQL Database</title> </head> <body> <?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = 'root@123'; $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysqli_error($conn)); } echo 'Connected successfully<br />'; $retval = mysqli_select_db( $conn, 'TUTORIALS' ); if(! $retval ) { die('Could not select database: ' . mysqli_error($conn)); } echo "Database TUTORIALS selected successfully\n"; mysqli_close($conn); ?> </body> </html>Output
Access the mysql_example.php deployed on apache web server and verify the output.
Database TUTORIALS selected successfullyI've managed to get into MySQL using the command line terminal, but when I tried to enter some SQL, it said 'no database selected'
how do I select a database? my database name is: photogallery
What code do I use to select it?
codeforester
35.7k16 gold badges102 silver badges126 bronze badges
asked Mar 13, 2011 at 2:36
Use USE. This will enable you to select the database.
USE photogallery;12.8.4: USE Syntax
You can also specify the database you want when connecting:
$ mysql -u user -p photogallery
answered Mar 13, 2011 at 2:37
Andrew MooreAndrew Moore
91.6k30 gold badges161 silver badges175 bronze badges
1
While invoking the mysql CLI, you can specify the database name through the -D option. From mysql --help:
-D, --database=name Database to use.I use this command:
mysql -h <db_host> -u <user> -D <db_name> -panswered Jul 2, 2020 at 23:47
codeforestercodeforester
35.7k16 gold badges102 silver badges126 bronze badges
2
Switch to a database.
mysql> use [db name];MySQL Commands
answered Mar 13, 2011 at 2:38
skazskaz
21.5k19 gold badges67 silver badges97 bronze badges
0
Hope this helps.
use [YOUR_DB_NAME];
answered Mar 13, 2011 at 2:37
0
Alternatively, you can give the "full location" to the database in your queries a la:
SELECT photo_id FROM [my database name].photogallery;If using one more often than others, use USE. Even if you do, you can still use the database.table syntax.
answered Jul 9, 2013 at 17:08
Nick TNick T
24.6k11 gold badges77 silver badges118 bronze badges
Use the following steps to select the database:
mysql -u username -pit will prompt for password, Please enter password. Now list all the databases
show databases;select the database which you want to select using the command:
use databaseName;select data from any table:
select * from tableName limit 10;You can select your database using the command use photogallery; Thanks !
answered Jul 13, 2017 at 5:42
USE database_name;
eg. if your database's name is gregs_list, then it will be like this >>
USE gregs_list;
smonff
3,2983 gold badges39 silver badges46 bronze badges
answered Jun 26, 2020 at 5:41