Jump to content

Print query results outside a loop.


kayla

Recommended Posts

As part of my uni project, I have created a system that enables users to edit grades.

The user will login and click on a student name, which will then take them to a form allowing them to edit and save the grades.

 

What I'm looking to do is to display the student name at the top of the editting page.

 

I've tried a couple ways of doing this, but they all seem to miss the first line of the table I'm displaying, as if its part of loop when the code is actually outside the code.

 

The code I have at the moment is:

 

<?php
// get user id
$Fid = $_GET['user_id'];

//declare database details
$db_host='127.0.01';
$db_database='college_81646';
$db_username='student';
$db_password='college';

//create connection
$connection= mysql_connect($db_host, $db_username, $db_password);
if(!$connection){
die ("Could not connect to the database: <br />".mysql_error());
}

//select database
$db_select = mysql_select_db($db_database);
if (!$db_select)
{
die ("Could not select the database: <br />".mysql_error());
}

//decalre the sql statement
$query="SELECT users.forename, users.surname, modules.module_name, grades.grade 
FROM users, modules, grades 
WHERE (users.user_id = grades.user_id) AND (modules.module_id = grades.module_id) AND users.user_id=".$Fid ;

//execute the query
$result = mysql_query( $query );
if (!$result)
{
   die ("Could not query the database: <br />". mysql_error());
}

echo "<center>";
$row = mysql_fetch_array($result);
echo "<b>".$row{'forename'};
echo "\n";
echo $row{'surname'};
echo "<br>";

echo("<table border='1' cellspacing='0' cellpadding='10' bordercolor='#6495ED' style='font-size: x-small; font-family: Verdana;'>");

echo ("<tr bgcolor='#CDCDCD' class='title'>
							<td width='120'><b>Module Name</b></td>
							<td width='60'><b>Grade</b></td>
							<td width='50'><b>Delete?</b></td>
							</tr>");
echo ("<form method='get' action='update_grade.php'>");

$count = 0;

while ($row = mysql_fetch_array($result))
{
echo ("<tr>");
echo "<td>".$row{module_name}."<br></td>";
echo "<td><input name='StudentGrade$count' size='10' maxlength='20' value='".$row{grade}."'<br></td>";
echo "<td><input type='checkbox' name='kill' value='kill'></td>";
echo ("</tr>");
$count = $count + 1;
}


echo ("</table>");
echo "<br><input type='submit' value ='submit' align='right'>";
echo "<input name='user_id' type='hidden' value='".$Fid."'>";
echo "</form>";
echo "</center>";




//close connection
mysql_close($connection);
?> 

 

The code which I have used to try to display the students name at the top of the page is:

 

echo "<center>";
$row = mysql_fetch_array($result);
echo "<b>".$row{'forename'};
echo "\n";
echo $row{'surname'};
echo "<br>";

 

If anybody can help me by telling me how to display the name without missing the first line of my table out then that would be a great help!

 

Thanks in advance! x

Link to comment
https://forums.phpfreaks.com/topic/188437-print-query-results-outside-a-loop/
Share on other sites

http://www.php.net/manual/en/function.mysql-data-seek.php

mysql_data_seek — Move internal result pointer

bool mysql_data_seek  ( resource $result  , int $row_number  )

 

mysql_data_seek() moves the internal row pointer of the MySQL result associated with the specified result identifier to point to the specified row number. The next call to a MySQL fetch function, such as mysql_fetch_assoc(), would return that row.

 

row_number starts at 0. The row_number should be a value in the range from 0 to mysql_num_rows() - 1. However if the result set is empty (mysql_num_rows() == 0), a seek to 0 will fail with a E_WARNING and mysql_data_seek() will return FALSE.

 

Enjoy.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.