PHP MySQL Functions to get records of a table as an array

mysql_fetch_row() function returns a record by taking a result identifier. Here it returns the set of data as an array. To get the value from the array we have to use array offset staring from 0. Each call to this mysql_fetch_row function returns the next record. Here one record is returned at a time and returns false if there is no more record to return. So we can easily use this function inside a while loop and display all the records. Each time at the starting of the while loop the mysql_fetch_row() function is checked and if true then the loop is executed and the records are displayed. Here is one example and we have used our student table for this.
<?
$query=mysql_query("select * from student");
echo mysql_error();
echo "<b>id,name,class,mark</b><br>";
while($nt=mysql_fetch_row($query)){
echo "$nt[0],$nt[1],$nt[2],$nt[3] <br>";
}
?>
Here is the result of above code

id,name,class,mark
1,John Deo,Four,75
2,Max Ruin,Three,85
3,Arnold,Three,55
4,Krish Star,Four,60
5,John Mike,Four,60
6,Alex John,Four,55
7,My John Rob,Fifth,78


If you are using PHP PDO then here is similar script.
<?Php
require "config.php";// Database connection

$sql="select * from student";
echo "<table>";
echo "<tr><th>id</th><th>name</th><th>class</th><th>mark</th></tr>";
foreach ($dbo->query($sql) as $row) {
echo "<tr ><td>$row[id]</td><td>$row[name]</td><td>$row[class]</td><td>$row[mark]</td></tr>";
}
echo "</table>";
?>
The SQL Dump is here for you to create the table and fill the data
CREATE TABLE student (
  id int(2) NOT NULL auto_increment,
  name varchar(50) NOT NULL default '',
  class varchar(10) NOT NULL default '',
  mark int(3) NOT NULL default '0',
  UNIQUE KEY id (id)
) ;

#
# Dumping data for table `student`
#

INSERT INTO student VALUES (1, 'John Deo', 'Four', 75);
INSERT INTO student VALUES (2, 'Max Ruin', 'Three', 85);
INSERT INTO student VALUES (3, 'Arnold', 'Three', 55);
INSERT INTO student VALUES (4, 'Krish Star', 'Four', 60);
INSERT INTO student VALUES (5, 'John Mike', 'Four', 60);
INSERT INTO student VALUES (6, 'Alex John', 'Four', 55);

PHP MySQL functions mysql_fetch_length() : Length of Data MySQL Error

Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com
rashed

21-10-2014

great post thanks




SQL Video Tutorials










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer