Hi,

In my webpage i created student details in that if i click view then i want to display all student name and their details one by one but in my code i typed everything but it displays first data only.Can anyone tell me how change this code.


this my code;

<?PHP 
include('connect.php');
echo $sql="select * from register";
$sql1=mysql_query($sql);
$sql2=mysql_fetch_array($sql1);
$sql3=mysql_fetch_row($sql1);
?>

Thanks in advance
Punitha pary

Member Avatar for Rhyan

You should have searched daniweb solution database more thoroughly. This question has been answered a thousand times at least.
Now, once again:
There are two basic ways you can do what you need. Both ways involve a cycle - e.g. while() statement or for() statement or foreach() statement.

The difference between the two approaches is the way you manipulate with the database data.
Approach 1
1st. you have to count how many records have been returned from you sql query by counting them

$results=mysql_num_rows($SQL1)

2nd. You have to fetch your results this many times in the following way:

for($i=0; $i<$results; $i++)
  {
  $row = mysql_fetch_assoc($sql1);
  echo $row['student_name'].'<br />'.$row['student_mark'];
  }

In this way you will have all student names and student marks displayed on your page.

The other approach is the following:
1st - same as approach 1;
2nd - you load all your DB data into one array like this

for($i=0; $i<$results; $i++)
  {
  $row[$i] = mysql_fetch_assoc($sql1);
  }

3rd - you manipulate with the loaded array with another cycle

for($i=0; $i<count($row); $i++)
  {
  $student = $row{$i];
  echo $student['student_name'].'<br />'.$student['student_mark'];
  }

The output will be the same, however approach no. 2 allows you to keep the database data loaded in your variable and you can reuse this data without querying the database again.

yes...
and also use while loop like:

while($sql2=mysql_fetch_array($sql1))
{
// display all students details
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.