Mysql_fetch_object( ) function in Php


Mysql_fetch_object ( ) function is also used to fetch or display data from database. This function is based on object oriented programming concept. In this fetch function we uses Php object sign followed by actual Mysql field name to fetch the data.

Mysql_fetch_object ( )
To perform the fetch function at first we need to establish the connection with Php without this we cannot send and fetch data from MYSQL.

connection.php
<?php
@mysql_connect('localhost','root',' ');     //password field is blank
@mysql_select_db('management');
?>
The above code establish the connection with Php and Mysql. Copy the above code in notepad and save as connection.php in a folder. To view more on connection.php click here

Creating Database and Table :-
Now open the PhpMyadmin using usermae as root and keep the password field blank and click on login. Now click on new located on left side and create a Database name it 'management'. Now create a table record under the management database.
Record Table
ID
NAME
EMAIL
ROOM_NUMBER
1
MUKESH SINGH
703
2
PRASHANT PASHAWAN
305
3
RANJAN KUMAR
102
4
ARPANA KUMARI
80
5
SOURAV GUPTA
600

SQL Query :-
The SQL query to fetch data from database
data.php
<?php
include_once('connection.php');
?>
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<div>
<?php
$query = mysql_query("select id,name,email,room_number from record");

$result = mysql_fetch_object($query);

echo "Id :- " . $result->id . "<br>";
echo "Name :- " . $result->name . "<br>";
echo "Email :- " . $result->email . "<br>";
echo "Room_Number  :- " . $result->room_number . "<br>";
echo "<hr>";

$result = mysql_fetch_object($query);
echo "Id :- " . $result->id . "<br>";
echo "Name :- " . $result->name . "<br>";
echo "Email :- " . $result->email . "<br>";
echo "Room_Number  :- " . $result->room_number . "<br>";

/*
if you use the mysql_fetch_row then we have to write the index value as
$result = mysql_fetch_row($query);
echo "Id :- " . $result[0] . "<br>";
echo "Name :- " . $result[1] . "<br>";
echo "Email :- " . $result[2] . "<br>";

if you are using the mysql_fetch_assoc then we have to write the field name as
$result = mysql_fetch_assoc($query);
echo "Id :- " . $result['id'] . "<br>";
echo "Name :- " . $result['name'] . "<br>";
echo "Email :- " . $result['email'] . "<br>";
echo "Room_Number  :- " . $result['room_number'] . "<br>";

if you are using the mysql_fetch_array then we have to write the field name as
echo "Id :- " . $result['id'] . "<br>";
echo "Name :- " . $result[1] . "<br>";
echo "Email :- " . $result['email'] . "<br>";
echo "Room_Number  :- " . $result[3] . "<br>";
*/
?>
</div>
</body>
</html>
Copy the above code in notepad and save as data.php in the same folder where connection.php file has been saved. Now run the data.php file in the browser.

Since the connection code is written in different file so we need to include the connection.php file to use in data.php file. You can also write the connection code in the data.php file but it is not good practice to use the connection code in the user interface page. To write the connection code separately also has an advantage, that we no need to write the same connection code several time for the several fetch function in a single page. if you simply include the connection file written somewhere else further that you no need to write the connection code several time for the several fetch function.

Explanation :- 
'$query' is a variable which store the SQL query and '$result ' is a variable which stores the data fetched from the SQL query. We can also perform the fetch function without using these variable. But using of variable makes the code shorter and easier we don't need to wright the same code again and again apart form that we just use the variable everywhere.

Mysql_fetch_object ( ) function fetches single record at a time, if you have 10 records in your database then you have to write the fetch statement 10  times to display all the 10 records. In the above example i have written the fetch function two time to fetch the two record. If you don't want to write the fetch statement several time you can use loop function. See below how to use the loop statement to display all the record in a single fetch statement.

<?php
include_once('connection.php');
?>
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<div>
<?php
$query = mysql_query("select id,name,email,room_number from record");
while($result = mysql_fetch_object($query))
{
echo "Id :- " . $result->id . "<br>";
echo "Name :- " . $result->name . "<br>";
echo "Email :- " . $result->email . "<br>";
echo "Room_Number  :- " . $result->room_number . "<br>";
echo "<hr>";
}
?>
</div>
</body>
</html>


Continue to
mysql_fetch_row( ) function
mysql_fetch_assoc( ) function

Mysql_fetch_array( ) function in Php


Similar to mysql_fetch_row ( ) function and mysql_fetch_assoc ( ) function, mysql_fetch_array ( ) function is also used to fetch data from database. There is no difference between among all these three fetch function. The only difference between these three function is, in mysql_fetch_row ( ) function we need to write the index value in the square braes to fetch the data and in mysql_fetch_assoc ( ) function actual mysql field name is written in the square braes to fetch the data and in mysql_fetch_array ( ) function, we can write both index value as well as field name in the square braes to fetch the data.

Mysql_fetch_array ( )
To perform the fetch function at first we need to establish the connection with Php without this we cannot send and fetch data from MYSQL.

connection.php
<?php
@mysql_connect('localhost','root',' ');     //password field is blank
@mysql_select_db('management');
?>
The above code establish the connection with Php and Mysql. Copy the above code in notepad and save as connection.php in a folder. To view more on connection.php click here

Creating Database and Table :-
Now open the PhpMyadmin using usermae as root and keep the password field blank and click on login. Now click on new located on left side and create a Database name it 'management'. Now create a table record under the management database.
Record Table
ID
NAME
EMAIL
ROOM_NUMBER
1
MUKESH SINGH
703
2
PRASHANT PASHAWAN
305
3
RANJAN KUMAR
102
4
ARPANA KUMARI
80
5
SOURAV GUPTA
600

SQL Query :-
The SQL query to fetch data from database
data.php
<?php
include_once('connection.php');
?>
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<div>
<?php
$query = mysql_query("select id,name,email,room_number from record");

$result = mysql_fetch_array($query);

echo "Id :- " . $result['id'] . "<br>";
echo "Name :- " . $result['name'] . "<br>";
echo "Email :- " . $result['email'] . "<br>";
echo "Room_Number  :- " . $result['room_number'] . "<br>";
echo "<hr>";
$result = mysql_fetch_array($query);
echo "Id :- " . $result['id'] . "<br>";
echo "Name :- " . $result['name'] . "<br>";
echo "Email :- " . $result['email'] . "<br>";
echo "Room_Number  :- " . $result['room_number'] . "<br>";

/*
if you use the mysql_fetch_row then we have to write the index value as
$result = mysql_fetch_row($query);
echo "Id :- " . $result[0] . "<br>";
echo "Name :- " . $result[1] . "<br>";
echo "Email :- " . $result[2] . "<br>";

if you are using the mysql_fetch_assoc then we have to write the field name as
$result = mysql_fetch_assoc($query);
echo "Id :- " . $result['id'] . "<br>";
echo "Name :- " . $result['name'] . "<br>";
echo "Email :- " . $result['email'] . "<br>";
echo "Room_Number  :- " . $result['room_number'] . "<br>";
*/
?>
</div>
</body>
</html>
Copy the above code in notepad and save as data.php in the same folder where connection.php file has been saved. Now run the data.php file in the browser.

Since the connection code is written in different file so we need to include the connection.php file to use in data.php file. You can also write the connection code in the data.php file but it is not good practice to use the connection code in the user interface page. To write the connection code separately also has an advantage, that we no need to write the same connection code several time for the several fetch function in a single page. if you simply include the connection file written somewhere else further that you no need to write the connection code several time for the several fetch function.

Explanation :- 
'$query' is a variable which store the SQL query and '$result ' is a variable which stores the data fetched from the SQL query. We can also perform the fetch function without using these variable. But using of variable makes the code shorter and easier we don't need to wright the same code again and again apart form that we just use the variable everywhere.

mysql_fetch_array ( ) function fetch all the data as an array to avoid the data conversion problem. This fetch function uses both index value or mysql actual field name to fetch the data.

Mysql_fetch_array ( ) function fetches single record at a time, if you have 10 records in your database then you have to write the fetch statement 10  times to display all the 10 records. In the above example i have written the fetch function two time to fetch the two record. If you don't want to write the fetch statement several time you can use loop function. See below how to use the loop statement to display all the record in a single fetch statement.


<?php
include_once('connection.php');
?>
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<div>
<?php
$query = mysql_query("select id,name,email,room_number from record");
while($result = mysql_fetch_array($query))
{
echo "Id :- " . $result['id'] . "<br>";
echo "Name :- " . $result['name'] . "<br>";
echo "Email :- " . $result['email'] . "<br>";
echo "Room_Number  :- " . $result['room_number'] . "<br>";
}
?>
</div>
</body>
</html>


Continue to
mysql_fetch_row( ) function
mysql_fetch_assoc( ) function

Mysql_fetch_assoc( ) function in Php


Just like mysql_fetch_row ( ) function mysql_fetch_assoc ( ) function is also used to fetch the data from database. there is no difference between these two function the resultant will be same in both the case. The only difference between these two function is, in mysql_fetch_row ( ) function we need to write the index value in the square braes to fetch the data and in mysql_fetch_assoc ( ) function actual mysql field name is written in the square braes to fetch the data.

Mysql_fetch_assoc ( )
To perform the fetch function at first we need to establish the connection with Php without this we cannot send and fetch data from MYSQL.

connection.php
<?php
@mysql_connect('localhost','root',' ');     //password field is blank
@mysql_select_db('management');
?>
The above code establish the connection with Php and Mysql. Copy the above code in notepad and save as connection.php in a folder. To view more on connection.php click here

Creating Database and Table :-
Now open the PhpMyadmin using usermae as root and keep the password field blank and click on login. Now click on new located on left side and create a Database name it 'management'. Now create a table record under the management database.
Record Table
ID
NAME
EMAIL
ROOM_NUMBER
1
MUKESH SINGH
703
2
PRASHANT PASHAWAN
305
3
RANJAN KUMAR
102
4
ARPANA KUMARI
80
5
SOURAV GUPTA
600

SQL Query :-
The SQL query to fetch data from database
data.php
<?php
include_once('connection.php');
?>
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<div>
<?php
$query = mysql_query("select id,name,email from record");

$result = mysql_fetch_assoc($query);
echo "Id :- " . $result['id'] . "<br>";
echo "Name :- " . $result['name'] . "<br>";
echo "Email :- " . $result['email'] . "<br>";
echo "<hr>";
$result = mysql_fetch_assoc($query);
echo "Id :- " . $result['id'] . "<br>";
echo "Name :- " . $result['name'] . "<br>";
echo "Email :- " . $result['email'] . "<br>";
echo "<hr>";
/*
if you use the mysql_fetch_row then we have to write the index value as
$result = mysql_fetch_row($query);
echo "Id :- " . $result[0] . "<br>";
echo "Name :- " . $result[1] . "<br>";
echo "Email :- " . $result[2] . "<br>";
*/
?>
</div>
</body>
</html>
Copy the above code in notepad and save as data.php in the same folder where connection.php file has been saved. Now run the data.php file in the browser.

Since the connection code is written in different file so we need to include the connection.php file to use in data.php file. You can also write the connection code in the data.php file but it is not good practice to use the connection code in the user interface page. To write the connection code separately also has an advantage, that we no need to write the same connection code several time for the several fetch function in a single page. if you simply include the connection file written somewhere else further that you no need to write the connection code several time for the several fetch function.


Explanation :- 
'$query' is a variable which store the SQL query and '$result ' is a variable which stores the data fetched from the SQL query. We can also perform the fetch function without using these variable. But using of variable makes the code shorter and easier we don't need to wright the same code again and again apart form that we just use the variable everywhere.

mysql_fetch_assoc ( ) function fetch all the data as an array to avoid the data conversion problem. This fetch function uses mysql actual field name to fetch the data. so in the above code the field name of id is 'id' and the field name of name is 'name' similarly the field name of email is 'email'.

Mysql_fetch_assoc ( ) function fetches single record at a time, if you have 10 records in your database then you have to write the fetch statement 10  times to display all the 10 records. In the above example i write the fetch function two time to fetch the two record. If you don't want to write the fetch statement several time you can use loop function. See below how to use the loop statement to display all the record in a single fetch statement.

<?php
include_once('connection.php');
?>
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<div>
<?php
$query = mysql_query("select id,name,email from record");
while($result = mysql_fetch_assoc($query))
{
echo "Id :- " . $result['id'] . "<br>";
echo "Name :- " . $result['name'] . "<br>";
echo "Email :- " . $result['email'] . "<br>";
echo "<hr>";
}
?>
</div>
</body>
</html>

Continue to
mysql_fetch_row( ) function
mysql_fetch_array( ) function
mysql_fetch_object( ) function

How to display data from database in Php


To display data from database Php has four inbuilt function
1) mysql_fetch_row( )
2) mysql_fetch_assoc( )
3) mysql_fetch_array( )
4) mysql_fetch_object( )

Here i will show you all the four fetch function one by one. Starting with the first fetch function

Mysql_fetch_row ( )
To perform the fetch function at first we need to establish the connection with Php without this we cannot send and fetch data from MYSQL.
connection.php
<?php
@mysql_connect('localhost','root',' ');     //password field is blank
@mysql_select_db('management');
?>
The above code establish the connection with Php and Mysql. Copy the above code in notepad and save as connection.php in a folder. To view more on connection.php click here

Creating Database and Table :-
Now open the PhpMyadmin using usermae as root and keep the password field blank and click on login. Now click on new located on left side and create a Database and name it 'management'. Now create a table record under the management database.
Record Table
ID
NAME
EMAIL
ROOM NO.
1
MUKESH SINGH
703
2
PRASHANT PASHAWAN
305
3
RANJAN KUMAR
102
4
ARPANA KUMARI
80
5
SOURAV GUPTA
600

SQL Query :-
The SQL query to fetch data from database
data.php
<?php
include_once('connection.php');
?>
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<div>
<?php
$query = mysql_query("select id,name,email,room_number from record");

$result = mysql_fetch_row($query);

echo "Id :- " . $result[0] . "<br>";
echo "Name :- " . $result[1] . "<br>";
echo "Email :- " . $result[2] . "<br>";
echo "<hr>";
$result = mysql_fetch_row($query);
echo "Id :- " . $result[0] . "<br>";
echo "Name :- " . $result[1] . "<br>";
echo "Email :- " . $result[2] . "<br>";
?>
</div>
</body>
</html>
Copy the above code in notepad and save as data.php in the same folder where connection.php file has been saved. Now run the data.php file in the browser.

Since the connection code is written in different file so we need to include the connection.php file to use in data.php file. You can also write the connection code in the data.php file but it is not good practice to use the connection code in the user interface page. To write the connection code separately also has an advantage, that we no need to write the same connection code several time for the several fetch function in a single page. if you simply include the connection file written somewhere else further that you no need to write the connection code several time for the several fetch function.

Explanation :- 
'$query' is a variable which store the SQL query and '$result ' is a variable which stores the data fetched from the SQL query. We can also perform the fetch function without using these variable. But using of variable makes the code shorter and easier we don't need to wright the same code again and again apart form that we just use the variable everywhere.

mysql_fetch_row ( ) function fetch all the data as an array to avoid the data conversion problem. This fetch function uses mysql field index value to fetch the data. An index number is a numeric value starts with zero ( 0 ) by default so in the above code the index value of id field is zero ( 0 ) and the index value of name field in 1 similarly the index value of email field is 2 and so on.

Mysql_fetch_row ( ) function fetches single record at a time, if you have 10 records in your database then you have to write the fetch statement 10  times to display all the 10 records. In the above example i have written the fetch function two time to fetch the two record. If you don't want to write the fetch statement several time you can use loop function. See below how to use the loop statement to display all the record in a single fetch statement.

<?php
include_once('connection.php');
?>
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<div>
<?php
$query = mysql_query("select id,name,email,room_number from record");
while($result = mysql_fetch_row($query))
{
echo "Id :- " . $result[0] . "<br>";
echo "Name :- " . $result[1] . "<br>";
echo "Email :- " . $result[2] . "<br>";
echo "<hr>";
}
?>
</div>
</body>
</html>

Continue to
2) mysql_fetch_assoc( )
3) mysql_fetch_array( )
4) mysql_fetch_object( )