Why echo failed on $rowcount?

73 views Asked by At

I'm implementing a thesis system using PHP and experiencing problems with echo $rowcount from database. Here is my coding:

<?php include "document.php";

echo '<article>';

function displaySearchOption()
{
    echo '<br><form action="documentList.php" method="post">
        Search:<br>
        <table border=1>
            <input type=text name = searchKey size=100 >
            <td><input type=submit name = searchByName value="Search Title"></td>
            <td><input type=submit name = searchByThesisId value="Search ID"></td>
            <td><input type=submit name = searchByLecturerName value="Search Advisor"></td>
            <td><input type=submit name = searchByStudentName value="Search Student"></td>
            <td><input type=submit name = searchByProgram value="Search Program"></td>
            <td><input type=submit name = searchByYear value="Search Year"></td>
            <td><input type=submit name = displayAll value="Show All"></td>
        </table>
        </form>';
}

$result = displaySearchOption();

displayAddNewThesisButton();

function displayAddNewThesisButton()
  {
  echo '<form action="addDocument.php" method="post">

    <input type=submit name =addNewThesis value="Add New Thesis">
    </form>';
  }


//to delete - 1
if (isSet($_POST['deleteThesis']))
{
    echo 'To delete staff: '. $_POST['thesisNumber'];
    deleteThesisRecord($_POST['thesisNumber']);
}
else if(isSet($_POST['searchByName']))
{
    echo 'To search document: '. $_POST['thesisNumber'];
    $result = getListOfThesisByDocumentName ($_POST['searchKey']);
}
else if(isSet($_POST['searchByThesisId']))
{
    echo 'To search document ID: '. $_POST['thesisNumber'];
    $result = getListOfThesisByThesisId($_POST['searchKey']);
}
else if(isSet($_POST['searchByLecturerName']))
{
    echo 'To search advisor: '. $_POST['thesisNumber'];
    $result = getListOfThesisByLecturerName($_POST['searchKey']);
}
else if(isSet($_POST['searchByStudentName']))
{
    echo 'To search student: '. $_POST['thesisNumber'];
    $result = getListOfThesisByStudentName($_POST['searchKey']);
}
else if(isSet($_POST['searchByProgram']))
{
    echo 'To search Program: '. $_POST['thesisNumber'];
    $result = getListOfThesisByProgram($_POST['searchKey']);
}
else if(isSet($_POST['searchByYear']))
{
    echo 'To search document year: '. $_POST['thesisNumber'];
    $result = getListOfThesisByDocumentYear($_POST['searchKey']);
}

else

    $result = getListOfThesis(); //call function in document.php
    $rowcount = mysqli_num_rows($result);

    echo '</br>' .$rowcount. '  records found';

if($rowcount != 0) //document record found
     //Display Table Header
        echo "<table border='1'>
        <tr>

            <th width='10%'>No</th>
            <th width='75%'>Thesis</th>
            <th width='30%'>Update/Delete</th>

        </tr>";
        $count=1;


while($row=mysqli_fetch_assoc($result)) //Display thesis information
    {
        $thesisNumber = $row['Thesis_no'];
        echo "<tr>";
        echo "<td><center>".$count."</center></td>";
        echo "<td> ID: " . $row['Thesis_no'].

        "</br></br><strong> Name: </strong>" . strtoupper($row['Student_name']).
        "</br></br><strong> Title: </strong>" . strtoupper($row['Thesis_name']).
        "</br></br><strong> Lecturer Name: </strong>" . strtoupper($row['Lecturer_name']).
        "</br></br><strong> Program: </strong>" . strtoupper($row['Program']).
        "</br></br><strong> Date: </strong>" . $row['Date'].
        "</br></br><strong> File Path: </strong>" . $row['filePath'].
        "</br></br><strong> Abstract: </strong>" . $row['Abstract'].
        "</td>";


        echo "</br>";

        //add button Update
        echo "<td><form action='updateDocumentForm.php' method='post' align='center'>
                    <input type='hidden' name='thesisNumber' value='$thesisNumber' />

                    <input type='submit' name='updateThesis' alt='Update' value='Update' />

                    </form>";

        //add button Delete
        echo "</br><form action='documentList.php' method='post' align='center'>
                    <input type='hidden' name='thesisNumber' value='$thesisNumber' />

                    <input type='submit' name='deleteThesis' alt='Delete' value='Delete' />

                    </form>";


        echo "</td>";
        echo "</tr>";
        $count++;
    }

mysqli_free_result($result); //free the result
mysqli_close($con); //close the connection

echo '</article>';

?>

The problem is when I try to echo $rowcount. I still can't figure out what is the cause of the problem. Anyone can help me?

Here is the coding where I echo $rowcount:

else if(isSet($_POST['searchByYear']))
{
    echo 'To search document year: '. $_POST['thesisNumber'];
    $result = getListOfThesisByDocumentYear($_POST['searchKey']);
}

else

    $result = getListOfThesis(); //call function in document.php
    $rowcount = mysqli_num_rows($result);

    echo '</br>' .$rowcount. '  records found';

and here is the function:

<?php
function getListOfThesis()
{
    $con = mysqli_connect('localhost','web2','web2','ethesisdb');
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " .
        mysqli_connect_error();
    }

    $sqlStr = "SELECT * FROM document order by Date";
    $qry = mysqli_query($con,$sqlStr );
    return $qry;
}

function deleteThesisRecord($Thesis_no)
{
    $con = mysqli_connect('localhost','web2','web2','ethesisdb');
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " .  mysqli_connect_error();
    }

    $sql="delete from document WHERE Thesis_no = '".$Thesis_no . "'";
    $qry = mysqli_query($con,$sql);

    if(!$qry)
    {
        echo 'error deleting record<br>';
        echo mysqli_error($con);
    }
    else
    {
        $count=mysqli_affected_rows($con);
        echo '<br>'.$count .' record/s deleted';
    }
    mysqli_close($con);
}


function getListOfThesisByThesisId($Thesis_no)
{
    $con = mysqli_connect('localhost','web2','web2','ethesisdb');
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        exit();
    }

    $sqlStr = "SELECT * FROM document ";
    $sqlStr = $sqlStr . " where Thesis_no = '".$Thesis_no."'";
    $qry = mysqli_query($con,$sqlStr );
    return $qry;
}

function getListOfThesisByDocumentYear($Date)
{
    $con = mysqli_connect('localhost','web2','web2','ethesisdb');
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        exit();
    }

    $sqlStr = "SELECT * FROM document ";
    $sqlStr = $sqlStr . " where Date = '".$Date."'";
    $qry = mysqli_query($con,$sqlStr );
    return $qry;
}

function getListOfThesisByDocumentName($Thesis_name)
{
    $con = mysqli_connect('localhost','web2','web2','ethesisdb');
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        exit();
    }
    $sqlStr = "SELECT * FROM document ";
    $sqlStr = $sqlStr . " where Thesis_name like '%".$Thesis_name."%'";
    $qry = mysqli_query($con,$sqlStr );
    return $qry;
}

function getListOfThesisByLecturerName($Lecturer_name)
{
    $con = mysqli_connect('localhost','web2','web2','ethesisdb');
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        exit();
    }
    $sqlStr = "SELECT * FROM document ";
    $sqlStr = $sqlStr . " where Lecturer_name like '%".$Lecturer_name."%'";
    $qry = mysqli_query($con,$sqlStr );
    return $qry;
}

function getListOfThesisByStudentName($Student_name)
{
    $con = mysqli_connect('localhost','web2','web2','ethesisdb');
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        exit();
    }
    $sqlStr = "SELECT * FROM document ";
    $sqlStr = $sqlStr . " where Student_name like '%".$Student_name."%'";
    $qry = mysqli_query($con,$sqlStr );
    return $qry;
}

function getListOfThesisByProgram($Program)
{
    $con = mysqli_connect('localhost','web2','web2','ethesisdb');
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        exit();
    }
    $sqlStr = "SELECT * FROM document ";
    $sqlStr = $sqlStr . " where Program like '%".$Program."%'";
    $qry = mysqli_query($con,$sqlStr );
    return $qry;
}

function updateThesisRecord($Thesis_no, $Thesis_name, $Lecturer_name, $Student_name, $Program, $Abstract, $Date)
{
    $con = mysqli_connect('localhost','web2','web2','ethesisdb');
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        exit();
    }


    $sqlStr = "update document ";

    $sqlStr = $sqlStr . " set Student_name = '".$Student_name."'";
    $sqlStr = $sqlStr . ", Lecturer_name ='".$Lecturer_name ."',Thesis_name ='".$Thesis_name."'";
    $sqlStr = $sqlStr . ", Program ='".$Program ."'";
    $sqlStr = $sqlStr . ", Abstract ='".$Abstract ."',Date ='".$Date."'";
    $sqlStr = $sqlStr . " where Thesis_no = '".$Thesis_no."'";


    $qry = mysqli_query($con,$sqlStr);
    return $qry;
}

function getThesisInformation($Thesis_no)
{
    $con = mysqli_connect('localhost','web2','web2','ethesisdb');
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $sqlStr = "SELECT * FROM document ";

    $sqlStr = $sqlStr . " where Thesis_no ='".$Thesis_no."'";

    $qry = mysqli_query($con,$sqlStr );
    return $qry;
}

function getThesisInformationResult($Thesis_no)
{
    $con = mysqli_connect('localhost','web2','web2','ethesisdb');
 if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $sqlStr = "SELECT * FROM document WHERE Thesis_no = '".$Thesis_no."'";
    $qry = mysqli_query($con,$sqlStr );
    return $qry;
}

function addNewThesisRecord($Thesis_no, $Thesis_name, $Lecturer_name, $Student_name, $Program, $Abstract, $Date, $filePath)
{
 $con = mysqli_connect('localhost','web2','web2','ethesisdb');
 if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  $sql="INSERT INTO document(Thesis_no, Thesis_name, Lecturer_name, Student_name, Program, Abstract, Date, filePath)
    VALUES ('$Thesis_no', '$Thesis_name', '$Lecturer_name', '$Student_name', '$Program', '$Abstract', '$Date', '$filePath')";
 $qry = mysqli_query($con,$sql);
 if(!$qry)
    return false; // error new staff record was not added
 else
    return true;
}

function generateNewThesisId()
    {
     $con = mysqli_connect('localhost','web2','web2','ethesisdb');
    if (mysqli_connect_errno())
        {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

    $sql= 'SELECT * FROM document';
    $qryResult=mysqli_query($con,$sql);

    $noOfRecord = mysqli_affected_rows($con)+1;
    $newThesisId = '10'.$noOfRecord;
    return $newThesisId;
    }
?>
0

There are 0 answers