Jump to content

Fetching rows from database


Roopavathy

Recommended Posts

Hello,

 

            I am very new to php programming.Please help me to display the students results from the database.I want to display all the papertitle,marks etc of a particular student.Here is my code

<?php
include("CommentBox/includes/Connection.php");
$exno=$_GET['examno'];

$query=mysql_query("Select * from results where Examno='$exno'",$con);
if (!$query)
{
die ("Problem in query".mysql_error());
}

while($row=mysql_fetch_array($query))
{
if($row['Examno']==$exno)
{
$Examno=$row['Examno'];
$studname=$row['Name'];
$studrno=$row['Rno'];
$studsem=$row['Sem'];

$papercode=$row['Subcode'];
$papertitle=$row['Title'];
$studcia=$row['CIA'];
$studese=$row['ESE'];
$studcredits=$row['Credits'];
$studgrade=$row['Grade'];
$studgradept=$row['Gradept'];

echo "<table>
<tr>
<td>Name:</td>
<td>".$studname."</td>
</tr>
<tr>
<td>Exam no:</td>
<td>".$Examno."</td>
</tr>
<tr>
<td>Roll No:</td>
<td>".$studrno."</td>
</tr>
<tr>
<td>Semester:</td>
<td>".$studsem."</td>
</tr></table>";
echo "<table>
<tr>
<td>Paper Code</td><td>Title</td><td>CIA</td>
<td>ESE</td><td>Credits</td><td>Grade</td>
<td>Grade Pt</td></tr>";
while($_GET['Examno']==$exno)
{

echo "<tr>
<td>".$papercode."</td><td>".$papertitle."</td><td>".$studcia."</td><td>".$studese."</td><td>".$studcredits."</td><td>".$studgrade."</td><td>".$studgradept."</td></tr>";

}


}
else
{
echo "Invalid Number!";
}
}

?>

post-164710-0-37148500-1376898937.jpg

Edited by Zane
Link to comment
Share on other sites

try this

$tableOne = "<table>
<tr>
<td>Name:</td>
<td>Exam no:</td>
<td>Roll No:</td>
<td>Semester:</td>
</tr>";


if (!empty($row)) {

    foreach ($row as $data) {
        $tableOne .= "<tr>
        <td>{$data['Name']}</td>
        <td>{$data['Examno']}</td>
        <td>{$data['Rno']}</td>
        <td>{$data['Sem']}</td>
        </tr>";
    }
}
$tableOne .= "</table>";


$tableTwo = "<table>
<tr>
<td>Paper Code</td>
<td>Title</td>
<td>CIA</td>
<td>ESE</td>
<td>Credits</td>
<td>Grade</td>
<td>Grade Pt</td>
</tr>";

if (!empty($row)) {

    foreach ($row as $data) {

        $tableTwo .= "<tr>
            <td>{$data['Subcode']}</td>
            <td>{$data['Title']}</td>
            <td>{$data['CIA']}</td>
            <td>{$data['ESE']}</td>
            <td>{$data['Credits']}</td>
            <td>{$data['Grade']}</td>
            <td>{$data['Gradept']}</td>
        </tr>";
    }
}

$tableTwo .= "</table>";


echo $tableOne;
echo "<br />";
echo $tableTwo;
Link to comment
Share on other sites

I have inserted the code inside my while loop but it displays  like this 

 

Warning: Illegal string offset 'Name' in C:\xampp\htdocs\COMMERCE\Resultsprocess.php on line 301 

 

and it goes on like this.

 

 

 

Here is my code:

 

 

<?php
include("CommentBox/includes/Connection.php");
$exno=$_GET['examno'];

$query=mysql_query("Select * from results where Examno='$exno'",$con);
if (!$query)
{
die ("Problem in query".mysql_error());
}

while($row=mysql_fetch_array($query))
{
$tableOne = "<table>
<tr>
<td>Name:</td>
<td>Exam no:</td>
<td>Roll No:</td>
<td>Semester:</td>
</tr>";


if (!empty($row)) {

foreach ($row as $data) {
$tableOne .= "<tr>
<td>{$data['Name']}</td>
<td>{$data['Examno']}</td>
<td>{$data['Rno']}</td>
<td>{$data['Sem']}</td>
</tr>";
}
}
$tableOne .= "</table>";


$tableTwo = "<table>
<tr>
<td>Paper Code</td>
<td>Title</td>
<td>CIA</td>
<td>ESE</td>
<td>Credits</td>
<td>Grade</td>
<td>Grade Pt</td>
</tr>";

if (!empty($row)) {

foreach ($row as $data) {

$tableTwo .= "<tr>
<td>{$data['Subcode']}</td>
<td>{$data['Title']}</td>
<td>{$data['CIA']}</td>
<td>{$data['ESE']}</td>
<td>{$data['Credits']}</td>
<td>{$data['Grade']}</td>
<td>{$data['Gradept']}</td>
</tr>";
}
}

$tableTwo .= "</table>";


echo $tableOne;
echo "<br />";
echo $tableTwo;


}

?>

Link to comment
Share on other sites

@Roopavathy, your code is not logical and some of it is not doing anything (that needs to be in there.)

 

line 13 (in your post) if($row['Examno']==$exno) is testing if the row matches $exno. you already know that will be true because that's the condition you put in your query.

 

line 50 (in your post) while($_GET['Examno']==$exno) is looping while the get variable matches $exno. again you already know that will be true because on line 3 in your posted code you have set $exno=$_GET['examno']; also, this while(){} loop will loop forever (or until php times out) because the condition being tested never changes.

 

next, you have 12 column values you are using inside of your loop. you don't need to create separate variables from each value (saving 12 lines of code). you can just use the $row['Examno']... variables directly in your code, especially since you are using each one only one time. you can also put php variables inside of double-quoted strings " ... " without using concatenation. an array variable like $row['Name'] does need {} around it when put into a string.

 

what your posted code would look like without the unnecessary bits -

include("CommentBox/includes/Connection.php");
$exno=$_GET['examno'];

$query=mysql_query("Select * from results where Examno='$exno'",$con);
if (!$query){
    die ("Problem in query".mysql_error($con));
}

while($row=mysql_fetch_assoc($query))
{
    echo "<table>
    <tr><td>Name:</td><td>{$row['Name']}</td></tr>
    <tr><td>Exam no:</td><td>{$row['Examno']}</td></tr>
    <tr><td>Roll No:</td><td>{$row['Rno']}</td></tr>
    <tr><td>Semester:</td><td>{$row['Sem']}</td></tr>
    </table>";
    echo "<table>
    <tr><td>Paper Code</td><td>Title</td><td>CIA</td><td>ESE</td><td>Credits</td><td>Grade</td><td>Grade Pt</td></tr>
    <tr><td>{$row['Subcode']}</td><td>{$row['Title']}</td><td>{$row['CIA']}</td><td>{$row['ESE']}</td>
    <td>{$row['Credits']}</td><td>{$row['Grade']}</td><td>$row['Gradept']</td></tr>
    </table>";
}

finally, is that your intended output? one table with the student information, followed by a second table with the exam information for that student, repeat both tables for each student?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.