We can retrieve the rows of the tables of a MySQL database in PHP language by using the mysql_fetch_array() function, mysqli_fetch_array() function or PDO_MYSQL driver in the form of arrays. These arrays can be either numeric arrays or associative arrays. The mysql_fetch_array() function has been deprecated in PHP 5.5.0 version and is completely removed in PHP 7.0.0 version. After PHP 5.5.0 version, mysqli_fetch_array() function should be used for fetching the array of rows in the resultset of the MySQL query. Alternatively, the PDO_MYSQL driver enables access from PHP to the database of Mysql and can be used to implement the PHP Data Objects interface. In this article, we will learn about the syntax of the mysqli_fetch_array() function and how this can be used to retrieve the result in the form of an array in PHP with the help of an example. Show Syntax of MySQL Fetch ArrayWhen we want to use this function in Object-oriented way we will have to follow the below syntax.
In the case of Procedural way, we will have to use the below syntax for fetching array containing rows of resultset of MySQL query – Start Your Free Data Science Course Hadoop, Data Science, Statistics & others All in One Data Science Bundle(360+ Courses, 50+ projects) Price 360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
Examples of MySQL Fetch ArrayLet us consider one example, firstly we will log in to MySQL and create one table named educba_writers in the database named educba, and then we will insert some records in the educba_writers table. After retrieving the resultset from the MySQL table, we will write the PHP program in which we will fetch the array of the rows that will be present in the educba_writers table. Select and use the educba database which we want to use for table creation using the following query statement – Execution of the above query gives the following output
Now, we will create the educba_writers table that will contain four columns namely id, firstName, rate, and joining_date using the CREATE TABLE statement in the following query –
Execution of the above query gives the following output – Let us insert some records in the educba_writers table using the INSERT INTO statement of Mysql in the following query –
Execution of the above query gives the following output: Now, we will select the data from educba_writers table by using the following query statement –
Execution of the above query gives the following output: It retrieves 4 rows with names payal, vyankatesh. Om Prakash and Om Prakash with ids 1,2,5 and 6. Note that the same data is to be retrieved in the array format when we use mysqli_fetch_array() function in PHP. Let us prepare one PHP file in our /var/www/html/ path named demo.php and write the code for establishing the connection with the educba database with the help of the user named username and password as the password of that user. Then we will write our SQL query to select the contents of the educba_writers table and finally retrieve the resultset from the query in an array format using mysqli_fetch_array() function and using the indexes of the array elements loop them into while loop and print the record of each row on a different line. The PHP code for this will be as specified below –
Save this file with the name of demo.php in the folder specified earlier. Now, we will run the PHP on the terminal itself and check the output. For this execute the following command on the terminal
Execution of the above query gives the following output: We can observe that the same four rows are retrieved in the array format and we have printed the contents storing each row in the array variable $row and then print the elements at 0th and 1st position which are id and firstName column values respectively until the result is present in the array of resultset returned from mysqli_fetch_array() function using the while loop. Note that the returned value of mysqli_fetch_array() function in our case if an array of arrays. In this way, we can retrieve the contents of the queries in the array format while using PHP. We can even specify the field names instead of indexes that are associative arrays in the demo.php file as shown below –
ConclusionWe can use mysqli_fetch_array() function in PHP 5.5.0 version and higher and mysql_fetch_array() function in the versions prior to PHP 7.0.0. mysql_fetch_array() function has been deprecated from 5.5 versions of PHP and completely removed from 7 and higher versions of PHP. Both these functions help in retrieving the resultset in the form of an array in PHP language and we can use numeric, associative of both types of indexing for the retrieved arrays. Recommended ArticlesThis is a guide to MySQL Fetch Array. Here we also discuss the introduction and syntax of mysql fetch array along with different examples and its code implementation. You may also have a look at the following articles to learn more – Jelaskan apa yang dimaksud MySQL fetch Row?Fungsi Mysql_fetch_row di PHP adalah sebagai perintah yang digunakan untuk menampilkan tiap-tiap baris data berdasarkan baris dan kolom tertentu pada tabel database sebagai hasil dari query mysql dengan argumen di dalamnya.
Apa itu mysql_query?MySQL Query adalah perintah atau instruksi yang dapat digunakan untuk mengelola database atau tabel dalam database MySQL. Query lebih dikenal dengan sebutan SQL (Structured Query Language) yang artinya adalah sebuah bahasa yang digunakan untuk mengakses data dalam basis data relasional.
Apa itu Mysqli_connect ()?Tentang Kode MySQLi
Fungsi utama yang digunakan di script ini adalah mysqli_connect(). Fungsi ini merupakan bagian internal PHP untuk membuat koneksi baru ke server MySQL.
Apa fungsi mysql_select_db?mysql_select_db adalah fungsi php untuk menjalankan argumen agar terkoneksi ke database mysql. Isi argumen tersebut adalah sebuah value nama database yang telah kita buat. Jika database yang kita buat memiliki nama db_akademik, maka argumen di dalam fungsi ini juga harus sama yaitu db_akademik.
|