查询为空错误

yyyllmsg  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(275)

**结案。**此问题不可复制或由打字错误引起。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

两年前关门了。
改进这个问题
当我将pdo语句放在搜索表单的if语句之外时,收到一个查询为空的错误。下面的代码可以正常工作,但在稍后尝试构建分页时会产生问题:

if (isset($_POST['submit'])) { 
    $countryname = $_POST['country'];
    $sql = "SELECT * from epic_schools_tbl where country = :countryname order by school_name";  
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(':countryname', $countryname);
    $stmt->execute();
    $row_count = $stmt->rowCount();
    if (isset($_POST['clear'])) { 
        $sql = "";
    }
}

下面的代码非常理想,但在prepare($sql)处绘制的查询为空错误:

if (isset($_POST['submit'])) { 
    $countryname = $_POST['country'];
    $sql = "SELECT * from epic_schools_tbl where country = :countryname order by school_name";  
} 
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':countryname', $countryname);
$stmt->execute();
$row_count = $stmt->rowCount();
if (isset($_POST['clear'])) { 
    $sql = "";
}

如何解决上述代码中的查询为空错误?

eqqqjvef

eqqqjvef1#

试试这个看是否有用。

<?php
    if (isset($_POST['submit'])) { 
        $stmt = $pdo->prepare("SELECT * FROM epic_schools_tbl WHERE country = :countryname ORDER BY school_name ASC");
        $stmt->bindParam(':countryname', $countryname);
        $countryname = $_POST['country'];
        if ($stmt->execute()) {
            $row_count = $stmt->rowCount();
            echo $row_count;
        } else {
            echo "Sorry could not retrieve records";
        }
    }
    ?>

例2更新

<?php
    if (!empty($_POST['country'])) {
        $sql = "SELECT * FROM epic_schools_tbl WHERE country = :countryname ORDER BY school_name"; 
    } else {
        echo "Sorry the country name is empty. Cannot continue";
    }
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(':countryname', $countryname);
    $countryname = $_POST['country'];
    if ($stmt->execute()) {
        $row_count = $stmt->rowCount();
        echo $row_count;
    } else {
        echo "Sorry could not retrieve records. Unable to perform query";
    }
?>

相关问题