PHP / MySQL 输出错误

Error in PHP / MySQL Output

我正在尝试获取我的数据库结果,但它没有显示任何内容。

请帮我找出错误:

 <?
     $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';
        }
    }    
?>

您在 sql 字符串和 bindValue() 中错误地绑定到变量。

变化:

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

搭配:

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

你的条件语句也不好。

同时更改:

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

收件人:

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