php - 如果未找到结果,则显示(消息)

标签 php sql error-handling pear

如果在搜索查询中未找到结果,则尝试显示一条消息。

这是我的代码的简短示例。

$sql = "SELECT * FROM details WHERE ID =1"

$res =& $db->query($sql);
if (PEAR::isError($res)) {
    die($res->getMessage());
}

while($row = $res->fetchRow())
{
     echo 'results'
    {

任何帮助将不胜感激!谢谢

最佳答案

DB_result有一个称为numRows()的方法,因此您可以检查
if($res->numRows() == 0)
http://pear.php.net/package/DB/docs/latest/DB/DB_result.html#methodnumRows

Get the number of rows in a result set

Return: the number of rows. A DB_Error object on failure.



而且您无需担心DB_Error,因为它已经在
PEAR::isError($res)

关于php - 如果未找到结果,则显示(消息),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10747661/

相关文章:

php - 尝试从 php 中的 session 中获取用户名

sql - 使多行的列值无效

sql - SQL 中的字符串剪切

sql - 复杂分组 - 设计/性能问题

asp.net - 如何在 Razor 页面中重定向到自定义404页面

python - mpi4py : abort() and raise RuntimeError()

php - 使用 Ajax 获得一组连续的 DB 派生单选按钮

php - 将查询转换为准备好的语句

error-handling - Camel 交易错误

php - 如何在 mac osx mamp 上为 php 安装 apc?