PHP and MySQL (49 Blogs) Become a Certified Professional

How to Implement Mysql_fetch_array in PHP

Published on Sep 25,2019 12 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

Fetching data is an important aspect when it comes to PHP. In this article, we will understand the mysql_fetch_array method in the following order:

 

What is Mysql_fetch_array?

The function returns a row from a recordset as an associative array and a numeric array or both. The function gets a row from the mysql_query() function and it returns an array if it’s TRUE And when there is a failure or when there are no more rows then FLASE.

sql_mysql

Mysql doesn’t have a Fetch array function. mysql_fetch_array is a PHP function that will allow you to access data stored in the result returned from the TRUE mysql_query if u want to know what is returned when you used the mysql_query function to query a Mysql database. It is not something that you can directly manipulate.

Syntax

mysql_fetch_array(data,array_type)

Data required which specifies data pointer to use and the data pointer is the result from the mysql_query() function

Array Types

Possible values are:

  • *mysql_assoc this is for an associative array
  • *mysql_num this is for a numeric array
  • *mysql_both this is for default or both

 

Code

<?php
$con=mysql_connect(“local12”,”AAAA”,zzzz12”);
If (!$con)
{
Die(‘could not connect: ‘ .mysql_error());
}
$db_selected=mysql_select_db(“test_db”,$con);
$sql=”CHOOSE*from person where lastname=’ Refsnes’ ’’ ;
$result=mysql_query($sql,$con);
Print_r(mysql_fetch_array($result));
Mysql_close($con);?>

 

Output:

Array

{

[0] => Refsnes

[lastname] => Refsnes

[1] =>seesee

[FirstName] => seesee

}

 

A Row of Data

The mysql_fetch_array function takes a Mysql query resource as an argument

($result)and it will return the first row of data returned by the mysql_query.

mysql_fetch_array-table

 

 

Getting Row data by mysql_fetch_array

Mysql_fetch_array will return the first row in a Mysql resource in the form of an associative array and columns of the MySQL result can be accessed by using the column names of the table.

PHP and MySQL Code for the above table

>?php
$query=”choose*from example”;
$result=mysql_query($query) or die (mysql_error());
$row=mysql_fetch_array($result) or die(mysql_error());
Echo$row[‘name’].”-“.$row[‘age’];
?>

Output:

asasas-23

 

With this, we come to an end of this mysqql_fetch_array article. I hope you got an idea of how you can use the mysql_fetch_array method in PHP to get the desired data.

Check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of this article and I will get back to you.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.