Introduction
In this article I describe the PHP MySQLi functions mysqli_get_server_info, mysqli_info, mysqli_get_server_version, mysqli_init and mysqli_insert_id. To learn some other MySQLi functions, go to:
- MySQLi Function in PHP: Part 1
- MySQLi Function in PHP: Part 2
- MySQLi Function in PHP: Part 3
- MySQLi Function in PHP: Part 4
- MySQLi Function in PHP: Part 5
- MySQLi Function in PHP: Part 6
- MySQLi Function in PHP: Part 7
PHP mysqli_get_server_info() Function
The PHP MySQLi "mysqli_get_server_info" function returns the version of the MySQL server or in other words this function returns a character string representing the server version.
Syntax
mysqli_get_server_info(connection) |
Parameter in mysqli_get_server_info function
The parameter of the function is:
Parameter |
Description |
connection |
It specifies the MySQL connection to use. |
Example
An example of the function is:
<?php
$conn=mysqli_connect("localhost","root","","mysql");
if (mysqli_connect_errno($conn))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo mysqli_get_server_info($conn);
mysqli_close($conn);
?>
Output
PHP mysqli_info() Function
The PHP MySQLi "mysqli_info" function provides the information about the most recent query and this function returns the information about the statement on success otherwise false on failure.
Syntax
mysqli_get_info(connection) |
Parameter in mysqli__info function
The parameter of the function is:
Parameter |
Description |
connection |
It specifies the MySQL connection to use. |
Example
An example of the function is:
<?php
$conn=mysqli_connect("localhost","root","","mysql");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Create table
$sqlqry="CREATE TABLE persons(FirstName CHAR(30),LastName CHAR(30),Age INT)";
// Execute query
if (mysqli_query($conn,$sqlqry))
{
echo "Table persons created successfully";
}
else
{
echo "Error creating table: " . mysqli_error($conn);
}
?>
Output
PHP mysqli_get_server_vesrion() Function
The PHP MySQLi "mysqli_get_server_version function returns the version of the MySQL server as an integer or in other words this function returns an integer representing the server version.
Syntax
mysqli_get_server_version(connection) |
Parameter in mysqli_get_server_vesrion function
The parameter of the function is:
Parameter |
Description |
connection |
It specifies the MySQL connection to use. |
Example
An example of the function is:
<?php
$conn=mysqli_connect("localhost","root","","mysql");
if (mysqli_connect_errno($conn))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo mysqli_get_server_version($conn);
mysqli_close($conn);
?>
Output
PHP mysqli_init() Function
The PHP MySQLi "mysqli_init" function initializes MySQLi and returns a resource for use with mysqli_real_connect() or in other words this function returns an object.
Syntax
Example
An example of the function is:
<?php
$conn=mysqli_init();
if (!$conn)
{
die("mysqli_init failed");
}
if (!mysqli_real_connect($conn,"localhost","root","","mysql"))
{
die("Connect Error: " . mysqli_connect_error());
}
echo 'hello Your connection established';
mysqli_close($conn);
?>
Output
PHP mysqli_insert_id() Function
The PHP MySQLi "mysqli_insert_id" function returns the auto generated id used in the last query or in other words this function returns the value of the auto_increment field that was updated by the previous query.
Syntax
mysqli_insert_id(connection) |
Parameter in mysqli_insert_id function
The parameter of the function is:
Parameter |
Description |
connection |
It specifies the MySQL connection to use. |
Example
An example of the function is:
<?php
$conn=mysqli_connect("localhost","root","","mysql");
if (mysqli_connect_errno($conn))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($conn,"INSERT INTO emp(id,name,salary)VALUES ('109','Rohan',8976)");
echo "New record has id: " . mysqli_insert_id($conn);
mysqli_close($conn);
?>
Output