Tuesday 3 December 2019

How to Display MySQL Results in PHP

In this article, we will learn about different functions that MySQLi provides in PHP. We will learn the syntax of these functions, what kind of errors these functions can give, under what situation these functions can give errors, and how can we resolve these errors. We will also see how these functions work and in what order do these functions display their results.

MySQLi

PHP is a scripting language that uses the MySQLi extension to connect to MySQL databases. PHP provides three different ways to connect to MySQL database which are MySQLi extension, MySQL extension, and PHP Data Objects (PDO). 

MySQLi_query

MySQLi_query is a function of the PHP MySQLi extension that runs the queries against the database and retrieves the data from it. This function takes in three parameters and out of these three parameters, one is optional. The syntax of this function is
Syntax

mysqli_query (connection, query, resultmode)

Where the connection is a required parameter for this function and it specifies the database with which the connection is to be established and it is done by declaring a variable as described below.

$connection = mysqli_connect ("localhost", "username", "password", "database_name");
The query is also a required parameter for this function which specifies the operation to be performed on the database such as SELECT, INSERT, etc. Result mode is not required for this function, it is optional and specifies the type of the result of the query. The default result mode for this function is MYSQLI_STORE_RESULT which transfers the set of results from the last query executed on the database connection. The PHP code below shows the implementation of this function.

<?php
$con = mysqli_connect ("localhost", "username", "password", "database"); 
if (mysqli_connect_errno ())
  {
  echo "Failed to connect to MySQL: “. mysqli_connect_error ();
  }

mysqli_query ($con, "SELECT * FROM Persons");
mysqli_query ($con, "INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Glenn','Quagmire',33)"); 
mysqli_close ($con);
?>

MySQLi_connect_error

This is a function of the MySQLi extension that returns a string which tells the user about the error that occurs during the connection to the database. This function does not take in any value as a parameter and just needs to be called in IF Else statement to check for the error is any. If no error occurred during the connection process then it will return null, otherwise it will return a string describing the error. The image below shows how to use this function.

<?php
$link = @mysqli_connect ('localhost', 'fake_user', 'my_password', 'my_db');
 if (! $link) {
        die ('Connect Error:'. mysqli_connect_error ());
}
?>

Result of MySQLi_connect_error
Result of MySQLi_connect_error

MySQLi_error

MySQLi_error is a function provided in the PHP MySQLi extension that can return the error details of the last function called. This function takes in only the connection variable as a parameter that stores the information about the connection to the database and just needs to be called in IF Else statement to check for the error is any. If there is no error occurred during the function call process then it will return null , otherwise, it will return a string describing the error. The image below shows how to use this function.

<?php
$mysqli = new mysqli ("localhost", "my_user", "my_password", "world"); 
if ($mysqli->connect_errno) {
        printf ("Connect failed: %s\n", $mysqli->connect_error);
        exit ();
if (! $mysqli->query ("SET a=1")) {
        printf ("Error message: %s\n", $mysqli->error);
$mysqli->close ();
?>
Result of MySQLi_errorResult of MySQLi_error

MySQLi_result 

This is a function that returns the result of the query which was run against the data of the database. There are many types of this function such as.
  • MySQLi_result:: current_field  It is a function that solitary takes one value as input and returns the present offset field of the present pointer.
  • MySQLi_result:: data_seek is a function that takes in just two input values and Adjusts the outcome pointer to a subjective row in the outcome.
  • MySQLi_result:: fetch_all is a function that takes in just two input values and brings all outcome pushes as an affiliated cluster, a numeric exhibit, or both.
  • MySQLi_result:: fetch_array is a function that takes in just two input values and brings all outcome pushes as an associative cluster, a numeric array, or both.
  • MySQLi_result:: fetch_assoc is a function that just requires two input values and finds an associative array in the result line.

0 comments:

Post a Comment