MySQL Connection using PHP Script

MySQL Connection using :

 PHP provides mysqli_connect() function to open a database connection. This function takes five parameters and returns a MySQL link identifier on success, or FALSE on failure.

Syntax:

 

mysqli_connect (server, user, password, new_link, client_flag);

 

Parameter Description
server Optional – The host name running database server. If not specified then default value is localhost:3036.

 

user Optional – The username accessing the database. If not specified then default is the name of the user that owns the server process.
password Optional – The password of the user accessing the database. If not specified then default is an empty password.

 

new link Optional – If a second call is made to mysqli_connect() with the same arguments, no new connection will be established; instead, the identifier of the already opened connection will be returned.

 

client_flags Optional – A combination of the following constants:

MYSQL_CLIENT_SSL – Use SSL encryption

MYSQL CLIENT COMPRESS – Use compression protocol MYSQL_CLIENT_IGNORE_SPACE – Allow space after function names.

MYSQL CLIENT INTERACTIVE – Allow interactive timeout seconds of inactivity before closing the connection

 

 

Disconnect from MVSQL database using PHP Script.

You can disconnect from MySQLi database anytime using PHP function mysql_close(). This function takes a single parameter.

Syntax:

mysql_close($link_identifier );

If a resource is not specified then last opened database is closed. This function returns true if it closes connection successfully otherwise it returns false.
Example:
Try out following example to connect to a MySQL server:
<?php
?>
$conn = mysqli_connect(“localhost”, “user”,”password”); if(! $conn)
}
die(‘Could not connect: . mysql_error());
echo ‘Connected successfully';
mysqli_close($conn);
Create Database using PHP Script:
PHP uses mysqli query function to create or delete a MySQL database. This function takes two parameters.
Syntax:
bool mysqli_query( sql, connection );
Parameter
sql
connection
Example:
Description
Required – SQL query to create or delete a MySQLi database
Optional – if not specified then last opened connection by mysqli_connect will be used.
Try out following example to create a database:
<?php
$conn = mysqli_connect(“localhost”, “user”, “password”); if(! $conn)
{
die(‘Could not connect: ‘. mysqli_error());
}
echo ‘Connected successfully
$create = ‘CREATE DATABASE organization';
$return = mysqli_query! create, Sconn 1;

if (!$return)
{
die(‘Could not create database: ‘ . mysql_error());
echo “Database organization created successfully\n”; mysql close($conn);
?>
Drop Database using PHP Script:
Syntax:
mysqli_query( sql, connection );

 

Parameter Description
sql Required – SQL query to create or delete a MySQL database
connection Optional – if not specified then last opened connection by mysql_connect will be used.

Example:
Try out following example to delete a database:
<?php
$conn mysql_connect(“localhost”,”user”,”password”); if (!$conn)
}
}
die(‘Could not connect: ‘. mysql_error());
echo ‘Connected successfully<br />';
$drop = ‘DROP DATABASE TUTORIALS';
$return= mysql_query( $drop, $conn); if (!$return)
}
}
die(‘Could not delete database: ‘ . mysql_error());
echo “Database TUTORIALS deleted successfully\n”; mysql_close($conn);
?>
WARNING: While deleting a database using PHP script, it does not prompt you for confirmation. So be careful while deleting a MySQL database.

if (!$return)
{
die(‘Could not create database: ‘ . mysqli_error());
echo “Database organization created successfully\n”; mysqli close($conn);
?>