MySQL Function in PHP:
In PHP Scripting language many functions are available for MySQL Database connectivity and executing SQL queries. MySQLi is extension in PHP scripting language which gives access to the MYSQL database. MySQLi extension was introduced version 5.0.0.
The MySQLi extension contains the following important functions which are related to MySQL database connectivity and management.
1. Mysqli_connect( ) Function
2. Mysqli_close( ) Function
3. mysqli_select_db( ) Function
4. mysqli_affected_rows( ) Function
5. mysqli_connect_error( ) Function
6. mysqli_fetch_assoc( ) Function
1. Database Connections:
Before accessing MySQL Database, connect to Database Server machine via PHP scripting language using Mysqli_connect() Function.
Syntax:
mysqli_connect(“Server Name” , “User Name” , “Password” , “DB Name”);
This function requires four parameters to connect to database server. Database Server name, Database username, password and Database Name.
2. Managing Database Connections:
The below code snippet describes managing database connection methods and features.
<?php
$servemame = “localhost”;
$usemame = “username”;
$password = “password”;
$DB_name = “School_DB”;
// Create connection
$conn = mysqli_connect($servemame, Susemame, $password,$DB_name);
The mysqli connect function uses these variables and connect Database server from PHP scripting. If connection gets fail, output will be printed with MySQL error code. Otherwise connection is success.
3. Performing Queries:
The main goal of MySQL and PHP connectivity is to retrieve and manipulate the data from MySQL database server. The SQL query statements are helping with PHP MySQL extension to achieve the objective of MySQL and PHP connection, “mysqliquery” is a function, helps to execute the SQL query statements in PHP scripting language.
Syntax:
mysqli_query(“Connection Object” , ’’SQL Query”)
Example:
$con=mysqli_connect(“localhost” , “my_user” , “my_pass word” , “Student_DB “); $sql= ”SELECT
student_name,student_age FROM
student”;mysqli_query($con,$sql);
4. Closing Connection:
mysqli_close( ) Function is used to close an existing opened database connection between PHP scripting and MySQL Database Server.