Search code examples
phpmysqlclip

Error in PHP / MySQL Output


I am trying to fetch my database results but it's not displaying any content.

Please help me to find the error:

 <?
     $pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
                                                // Search from MySQL database table
$search=$_POST['search'];
$query = $pdo->prepare("select * from usermark where sid LIKE '%$search%' LIMIT 0 , 1");
$query->bindValue(1, "%$search%", PDO::PARAM_STR);
$query->execute();
                                            if (!$query->rowCount() == 0) {

    echo "<tr><td colspan='2' bgcolor='#800000'><p align='center'><font face='Verdana' color='#FFFFFF'>RESULTS</font></td></tr>";
    echo "<tr><td width='29%'></td><td width='69%'></td></tr>";
    while ($results = $query->fetch()) {
    echo "<tr><td width='29%'>NAME</td><td width='69%'></td></tr>";
    echo "<tr><td width='29%'>ID</td><td width='69%'>";
    echo $results['sid'];
    echo "</td></tr><tr><td width='29%'>ROLL NO.</td><td width='69%'></td></tr>";
    echo "<tr><td width='29%'>OMR NO.</td><td width='69%'>";
    echo $results['somr'];
    echo "</td></tr><tr><td width="29%"></td><td width="69%"></td></tr>";
    echo "<tr><td width="29%">TOTAL MARKS</td><td width="69%">";
    echo $results['smark'];
    echo "</td></tr><tr><td width='29%'>MARKS OBTAINED</td><td width='69%'></td></tr>";
    echo "<tr><td width='29%'>PERCENTAGE</td><td width='69%'></td></tr>";


                echo "</table>";        
        } else {
            echo 'Nothing found';
        }
    }    
?>

Solution

  • You're binding to a variable incorrectly, in your sql string and in bindValue().

    Change:

    $query = $pdo->prepare("select * from usermark where sid LIKE '%$search%' LIMIT 0 , 1");
    $query->bindValue(1, "%$search%", PDO::PARAM_STR);
    

    With:

    $query = $pdo->prepare("select * from usermark where sid LIKE '%:search%' LIMIT 0 , 1");
    $query->bindValue(":search", $search, PDO::PARAM_STR);
    

    You also have a bad conditional statement.

    Also Change:

    if (!$query->rowCount() == 0) {
    

    To:

    if ($query->rowCount() != 0) {