Hello Coders, here are not a very new topic for the experienced people but a very interesting topic for beginners who are always confused with these function as I mention on heading.
Today I elaborate all these with some small examples, I think you all understand very well after reading that one time. So let’s start.
Here is a table for better understanding, Suppose this is MySQL table and there is one row.
mytable.sql
id | username | password |
---|---|---|
1 | phpcodertech | 123456 |
mysql_fetch_row()
This function as the name suggests giving rows of the data which we set in the query. But one thing the values will come in order as we define on the query and indexed start with 0 and end with less than a number of the selected column.
Here is the example which shows you how this is implemented on the code.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php //this is you connection file included include('connect.php'); $query=mysql_query("select * from mytable"); $row=mysql_fetch_row($query); //fething their data using array index echo $row[0]; echo $row[1]; echo $row[2]; ?> Output: 1 phpcodertech 123456 |
mysql_fetch_aasoc()
This is the second PHP function which is also used for data fetching but with different format. This is also called the ASSOCIATIVE array format. Means on this array, all elements have their name with values. We fetch all value using their field name.
Like the following example,
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php include ('connect.php'); $query = mysql_query("select * from mytable"); //This is how we use this function $row=mysql_fetch_assoc($query); //after query fetching data using their field name echo $row['id']; echo $row['username']; echo $row['password']; ?> Output: 1 phpcodertech 123456 |
mysql_fetch_object()
All the above functions are using to fetch data from the database but with a different type. This function also does the same but fetches data from the database using Object type.
Please check the example below for a better understanding.
1 2 3 4 5 6 7 8 9 10 11 |
<?php include('connect.php'); $query=mysql_query("select * from mytable"); //this is the source how we implement it $row=mysql_fetch_object($query); //on this fetch using object type echo $row->id; echo $row->username; echo $row->password; ?> Output: 1 phpcodertech 123456 |
mysql_fetch_array()
This function, fetch result from a database as an associative array, index numeric array and both numeric and associative at the same code.
See here on the example,
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php include('connect.php'); $query=mysql_query("select * from tb"); $row=mysql_fetch_array($query); echo $row['id']; echo $row['username']; echo $row['password']; //On this both numeric and associative array will work echo $row[0]; echo $row[1]; echo $row[2]; ?> |
Above all are the difference between these functions, check if you have some improvements then please comment below.
To know more https://www.php.net/manual/en/ref.mysql.php
Also Check:
- How JavaScript For Loop Works (Complete Guide With Example)
- Create Repository & Push Code To GitHub First Time
- Migrate WordPress Site To New Host Manually
- Set-up Firebase Project using Firebase Console
Happy Coding…!
[…] mysql_fetch_row() VS mysql_fetch_object() VS mysql_fetch_assoc() VS mysql_fetch_array() […]