user3186314 user3186314 - 3 months ago 9
SQL Question

having difficulties in php mysql

I am not sure which title to give these challenges but it goes thus,

I am working on a project and am having 2 main challenges which are:

  1. In the database, some courses value are NULL, and I don't want to print courses that hava a NULL value.
    I don't know how I can achieve this in php or mysql. I need something like so:

    if(course1!=NULL || course2!=NULL || course3!=NULL || course4!=NULL){
    // echo course1; echo course2; echo course3; echo course4; }

The database looks like this:

The database

And the output so far look like this:

  1. A remark() function which check whether the course is A, B, C, D, E, or F is not working properly.
    I want the remark() function to output either Excellent, Credit, Pass, or fail base on the grade.

    Follows is the code:

    function remark(){

    global $mathematics, $physics, $chemistry, $lang;

    if($mathematics=='A' || $physics=='A' || $chemistry=='A' || $lang=='A'){
    $remark = "Excellent";

    elseif($mathematics=='B' || $physics=='B' || $chemistry=='B' || $lang=='B' || $mathematics=='C' || $physics=='C' || $chemistry=='C' || $lang=='C'){
    $remark = "Credit";
    elseif($mathematics=='D' || $physics=='D' || $chemistry=='D' || $lang=='D' || $mathematics=='E' || $physics=='E' || $chemistry=='E' || $lang=='E'){
    $remark = "Pass";
    $remark = "Fail";
    return $remark;
    if(isset($_POST['student_id']) && !empty($_POST['student_id'])){$student_id = $_POST['student_id'];

    $check = "SELECT * FROM sample1 WHERE student_id='$student_id'";
    $check_query = mysqli_query($connection, $check);

    while($query_row = mysqli_fetch_assoc($check_query)){

    $mathematics = $query_row['mathematics'];
    $physics = $query_row['physics'];
    $chemistry = $query_row['chm'];
    $lang = $query_row['lang'];

    echo "<table border=1 width=50%>
    <tr> <td>Name: ".$query_row['name']." </td>
    <td>Student Id: ".$query_row['student_id']."</td>
    <td>Level: ".$query_row['level']."</td>
    <th> Course </th>
    <th> Grade </th>
    <th> Remark </th>

    <tr> <td>Mathematics </td> <td>".$mathematics." </td></td> <td>".remark()." </td></tr>
    <tr> <td>Physics </td> <td>".$physics."</td> </td><td>".remark()." </td></tr>
    <tr> <td>Chemistry </td> <td>".$chemistry."</td> </td><td>".remark()." </td></tr>
    <tr> <td>Language </td> <td>".$lang."</td> </td><td>".remark()." </td></tr>

    else{echo "Record not found";}


About the MYSQL not null values, you use this:

SELECT * FROM yourtable WHERE course1 IS NOT NULL

So, IS NOT NULL will give you queries where that column (course1) is not null. Here more info.

And for PHP, your remark() function has a wrong approach. I will give you one that is correct for your purpouse.

function remark($course){

    switch($course) {

        case 'A':
            return 'Excellent';

        case 'B':
        case 'C':
            return 'Credit';

        case 'D':
        case 'E':
            return 'Pass';

            return 'Fail';



So in your HTML table you do this for each course:

while ($query_row = mysqli_fetch_assoc($check_query)) {

    $mathematics = $query_row['mathematics'];
    $physics     = $query_row['physics'];
    $chemistry   = $query_row['chm'];
    $lang        = $query_row['lang'];

    echo "<table border=1 width=50%>
        <tr> <td>Name: " . $query_row['name'] . "  </td>
            <td>Student Id: " . $query_row['student_id'] . "</td>
            <td>Level: " . $query_row['level'] . "</td>
            <th> Course </th>
            <th> Grade </th>
            <th> Remark </th>

        if(!is_null($mathematics)) echo "<tr> <td>Mathematics </td> <td>" . $mathematics . " </td></td> <td>" . remark($mathematics) . " </td></tr>";
        if(!is_null($physics)) echo "<tr> <td>Physics </td> <td>" . $physics . "</td> </td><td>" . remark($physics) . " </td></tr>";
        if(!is_null($chemistry)) echo "<tr> <td>Chemistry </td> <td>" . $chemistry . "</td> </td><td>" . remark($chemistry) . " </td></tr>";
        if(!is_null($lang)) echo "<tr> <td>Language </td> <td>" . $lang . "</td> </td><td>" . remark($lang) . " </td></tr>";

    echo "</table>";