PHP Functions for MySQL Connectivity and Operation
PHP and MySQL are free and open source. The combination of both PHP and MySQL provides a powerful option for web development and is most popular. We can connect PHP and MySQL using MySQLi object-oriented procedure or MySQLi procedural procedure or PDO procedure. We mainly discuss the MySQLi procedural procedure here.
'mysqli' is an improved version of the older PHP MySQL driver, offering various benefits and we recommend using ‘mysqli’ when dealing with MySQL server versions 4.1.3 and newer. The old MySQL extension was deprecated in PHP 5.5.0 and was removed in PHP 7.
mysqli_connect() Function
mysqli_connect(host, username, password, dbname, port, socket);
mysqli_connect() returns an object representing the connection to the MySQL server.
Parameter | Description |
---|---|
host | Specifies a host name or an IP address |
username | Specifies the MySQL username |
password | Specifies the MySQL password |
dbname | Specifies the default database to be used |
port | Specifies the port number to attempt to connect to the MySQL server |
socket | Specifies the socket or named pipe to be used |
Eg:
<?php
$connection = mysqli_connect("localhost","root","password","db_name");
// Check connection
if (mysqli_connect_errno())
{
echo "MySQL connection failed: " . mysqli_connect_error();
exit();
}
else
{
echo "MySQL connection successful";
}
?>
mysqli_select_db() Function
mysqli_select_db(connection_variable, database_name);
It returns boolean values (TRUE on success. FALSE on failure).
Parameter | Description |
---|---|
connection_variable | Specifies the MySQL connection to use |
database_name | Specifies the database name to be connected |
Eg:
<?php
$connection = mysqli_connect("localhost","root","pwd","db_test");
mysqli_select_db($connection, "db_test"); // selects the database 'db_test'.
mysqli_close($connection); // Close the connection to the database.
?>
mysqli_query() Function
mysqli_query(connection_variable, query, result-mode);
For successful SELECT, SHOW, DESCRIBE, or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other queries it will return TRUE or FALSE.
Parameter | Description |
---|---|
connection_variable | Specifies the MySQL connection to use |
query | Specifies the SQL query string |
result-mode (not necessary) |
MYSQLI_STORE_RESULT (default) MYSQLI_USE_RESULT MYSQLI_ASYNC |
Eg:
<?php
$connection = mysqli_connect("localhost","root","pwd","db_test");
$result = mysqli_query($connection, "SELECT * FROM test_table");// returns result object
mysqli_close($connection); // Close the connection to the database.
?>
mysqli_fetch_row() Function
mysqli_fetch_row(result);
It returns an enumerated array representing the fetched row, null if there are no more rows in the result set, or false on failure.
Parameter | Description |
---|---|
result | Specifies a result set |
Eg:
<?php
$connection = mysqli_connect("localhost","root","pwd","db_test");
$result = mysqli_query($connection, "SELECT * FROM test_table");
while ($each_row = mysqli_fetch_row($result))
{
echo $each_row[0];
}
mysqli_close($connection); // Close the connection to the database.
?>
mysqli_fetch_array() Function
mysqli_fetch_array(result,result-type);
It returns an array representing the fetched row, null if there are no more rows in the result set, or false on failure.
Parameter | Description |
---|---|
result | Specifies a result set |
result-type | Specifies what type of array that should be produced. Can be one of the following values: MYSQLI_ASSOC MYSQLI_NUM MYSQLI_BOTH ( default) |
Eg:
<?php
$connection = mysqli_connect("localhost","root","pwd","db_test");
$result = mysqli_query($connection, "SELECT * FROM test_table");
while ($arr = mysqli_fetch_array($result))
{
echo $arr[0];
}
mysqli_close($connection); // Close the connection to the database.
?>
mysql_result() Function
mysql_result(data,row,field);
As opposed to mysql_result(), there's no mysqli_result() function available in MySQLi. Use mysqli_fetch_array() function.
The mysql_result() function returns the contents of one cell from a MySQL result set on success, or false on failure.
Parameter | Description |
---|---|
data | Specifies which result handle to use. |
row | Specifies which row number to get. Row numbers start at 0. |
field | It can be the field's offset, the field's name, or tablename.fieldname. If undefined, the first field is retrieved. |
mysql_list_fields() Function
mysql_list_fields (database_name,table_name [, resource link_identifier]);
mysql_list_fields lists MySQL table fields. It returns a mysql result set for success. NULL if an error occurred. The function mysql_list_fields() is deprecated in PHP 5.4.0, and removed in PHP 7.0.0.
Parameter | Description |
---|---|
database_name | The name of the database that is being queried. |
table_name | The name of the table that is being queried. |
link_identifier | The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. |
SHOW COLUMNS FROM table_name FROM db_name;
mysqli_num_fields() Function
mysqli_num_fields(result);
The mysqli_num_fields() returns the number of fields in a result set.
Parameter | Description |
---|---|
result | Specifies a result set |
Eg:
<?php
$connection = mysqli_connect("localhost","root","pwd","db_test");
$result = mysqli_query($connection, "SELECT * FROM test_table");
$columns = mysqli_num_fields($result);
echo "Total fields in the result is:".$columns;
mysqli_close($connection); // Close the connection to the database.
?>