Subscribe For Free Updates!

We'll not spam mate! We promise.

Friday, October 31, 2014

Selecting MySQL Database

Once you get connection with MySQL server, it is required to select a particular database to work with. This is because there may be more than one database available with MySQL Server.

Selecting MySQL Database from Command Prompt:

This is very simple to select a particular database from mysql> prompt. You can use SQL command use to select a particular database.

Example:

Here is an example to select database called JAVATUTORIALS:
[root@host]# mysql -u root -p
Enter password:******
mysql> use JAVATUTORIALS;
Database changed
mysql> 
Now, you have selected JAVATUTORIALS database and all the subsequent operations will be performed on JAVATUTORIALS database.
NOTE: All the database names, table names, table fields name are case sensitive. So you would have to use proper names while giving any SQL command.

Selecting MySQL Database Using PHP Script:

PHP provides function mysql_select_db to select a database. It returns TRUE on success or FALSE on failure.

Syntax:

bool mysql_select_db( db_name, connection );

Parameter Description
db_nameRequired - MySQL Database name to be selected
connectionOptional - if not specified, then last opened connection by mysql_connect will be used.

Example:

Here is the example showing you how to select a database.
<html>
<head>
<title>Selecting MySQL Database</title>
</head>
<body>
<?php
$dbhost = 'localhost:3036';
$dbuser = 'test';
$dbpass = 'test123';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db( 'JAVATUTORIALS' );
mysql_close($conn);
?>
</body>
</html>

Socializer Widget
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

0 comments:

Post a Comment