Olpt2 Chapter 6
Olpt2 Chapter 6
Objectives:
a. Discuss the overview of MySQLi.
b. Recognize the syntax for managing a database.
c. Create a database connection using MySQLi.
Page | 1
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2
Page | 2
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2
}
// Otherwise MySQL or PHP Statements
?>
</body>
</html>
MySQL Connection Using PHP Script
PHP provides mysqli contruct or mysqli_connect() function to open a database connection. This
function takes six parameters and returns a MySQL link identifier on success or FALSE on failure.
Syntax
$mysqli = new mysqli($host, $username, $passwd, $dbName, $port, $socket);
1 $host
Optional − The host name running the database server. If not specified, then the default
value will be localhost:3306.
2 $username
Optional − The username accessing the database. If not specified, then the default will be
the name of the user that owns the server process.
3 $passwd
Optional − The password of the user accessing the database. If not specified, then the
default will be an empty password.
4 $dbName
Optional − database name on which query is to be performed.
5 $port
Optional − the port number to attempt to connect to the MySQL server..
Page | 3
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2
6 $socket
Optional − socket or named pipe that should be used.
You can disconnect from the MySQL database anytime using another PHP function close().
Syntax
$mysqli→close();
Example
Try the following example to connect to a MySQL server −
Copy and paste the following example as mysql_example.php −
<html>
<head>
<title>Connecting MySQL Server</title>
</head>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root@123';
$mysqli = new mysqli($dbhost, $dbuser, $dbpass);
if($mysqli→connect_errno ) {
printf("Connect failed: %s<br />", $mysqli→connect_error);
exit();
}
printf('Connected successfully.<br />');
$mysqli→close();
?>
</body>
Page | 4
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2
</html>
Output
Access the mysql_example.php deployed on apache web server and verify the output.
Connected successfully.
For more info about MySQLi Overview, please click the link below:
https://www.youtube.com/watch?v=BEbKji_pSZM
1 $sql
Required - SQL query to create a MySQL database.
2 $resultmode
Page | 5
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2
Example
Try the following example to create a database −
Copy and paste the following example as mysql_example.php −
<html>
<head><title>Creating MySQL Database</title></head>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root@123';
$mysqli = new mysqli($dbhost, $dbuser, $dbpass);
if($mysqli→connect_errno ) {
printf("Connect failed: %s<br />", $mysqli→connect_error);
exit();
}
printf('Connected successfully.<br />');
Page | 6
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2
$mysqli→close();
?>
</body>
</html>
Output
Access the mysql_example.php deployed on apache web server and verify the output.
Connected successfully.
Database TUTORIALS created successfully.
Drop a Database using mysqladmin
You would need special privileges to create or to delete a MySQL database. So, assuming you
have access to the root user, you can create any database using the mysql mysqladmin binary.
Be careful while deleting any database because you will lose your all the data available in your
database.
Here is an example to delete a database(TUTORIALS) created in the previous chapter −
[root@host]# mysqladmin -u root -p drop TUTORIALS
Enter password:******
This will give you a warning and it will confirm if you really want to delete this database or not.
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.
Page | 7
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2
1 $sql
Required - SQL query to drop a MySQL database.
2 $resultmode
Optional - Either the constant MYSQLI_USE_RESULT or MYSQLI_STORE_RESULT depending
on the desired behavior. By default, MYSQLI_STORE_RESULT is used.
Example
Try the following example to drop a database −
Copy and paste the following example as mysql_example.php −
<html>
<head><title>Dropping MySQL Database</title></head>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root@123';
$mysqli = new mysqli($dbhost, $dbuser, $dbpass);
if($mysqli->connect_errno ) {
printf("Connect failed: %s<br />", $mysqli->connect_error);
exit();
}
printf('Connected successfully.<br />');
Page | 8
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2
}
if ($mysqli->errno) {
printf("Could not drop database: %s<br />", $mysqli->error);
}
$mysqli->close();
?>
</body>
</html>
Output
Access the mysql_example.php deployed on apache web server and verify the output.
Connected successfully.
Database TUTORIALS dropped successfully.
For more info about Creating and Drop Database, please click the link below:
https://www.youtube.com/watch?v=2SYFnVenxRw
Page | 9
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2
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 ) : bool
1 $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';
Page | 10
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2
$dbpass = 'root@123';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysqli_error($conn));
}
echo 'Connected successfully<br />';
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 successfully
Create Table
To begin with, the table creation command requires the following details −
• Name of the table
• Name of the fields
• Definitions for each field
Syntax
Page | 11
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2
Page | 12
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2
1 $sql
Required - SQL query to create a MySQL table.
2 $resultmode
Optional - Either the constant MYSQLI_USE_RESULT or MYSQLI_STORE_RESULT depending
on the desired behavior. By default, MYSQLI_STORE_RESULT is used.
Example
Try the following example to create a table −
Copy and paste the following example as mysql_example.php −
<html>
<head>
<title>Creating MySQL Table</title>
Page | 13
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2
</head>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root@123';
$dbname = 'TUTORIALS';
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if($mysqli→connect_errno ) {
printf("Connect failed: %s<br />", $mysqli→connect_error);
exit();
}
printf('Connected successfully.<br />');
Page | 14
MODULE INTEGRATIVE PROGRAMMING TECHNOLOGIES - 2
$mysqli→close();
?>
</body>
</html>
Output
Access the mysql_example.php deployed on apache web server and verify the output.
Connected successfully.
Table tutorials_tbl created successfully.
For more info about Tables, please click the link below:
https://www.youtube.com/watch?v=zYNAfEHGXAg
REFERENCES
https://www.tutorialspoint.com/mysqli/mysqli_introduction.htm
https://www.tutorialspoint.com/mysqli/mysqli_php_syntax.htm
https://www.tutorialspoint.com/mysqli/mysqli_create_database.htm
https://www.tutorialspoint.com/mysqli/mysqli_select_database.htm
https://www.tutorialspoint.com/mysqli/mysqli_create_tables.htm
Page | 15