PDO版本的mysql_num_rows($ result)== 0) [英] PDO version of mysql_num_rows($result)==0)

查看:109
本文介绍了PDO版本的mysql_num_rows($ result)== 0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
使用PDO替代mysql_num_rows

Possible Duplicate:
Alternative for mysql_num_rows using PDO

^我相信这不是一个相同的问题-其他作者的代码与我的不同,因此需要不同的答案.我成功地从这篇文章中得到了答案,并将其标记为已回答.现在一切正常(没有其他重复"线程的帮助.

^ I believe it isn't the same question - The other authors code is different to mine, which needed a different answer. I successfully got my answer from this post and marked it as answered. Everything is working fine now (no help from the other 'duplicate' thread.

如果未找到结果,我想显示找不到客户端"消息,以下代码是否有PDO方法?

I want to display a "No Client Found" message if no results are found, Is there a PDO method to the following code?:

$result = mysql_query($sql) or die(mysql_error()."<br />".$sql);
if(mysql_num_rows($result)==0) {
    echo "No Client Found";

我尝试了以下方法...

I tried the following...

<?php                               
$db = new PDO('mysql:host=localhost;dbname=XXXXXXXXXXXX;charset=utf8','XXXXXXXXXXXX', 'XXXXXXXXXXXX');

    $query = $db->query('SELECT * FROM client');

    if ($query == FALSE) {
      echo "No Clients Found";
    }
    else
    {
    foreach($query as $row)
    {
     <some code here>
    }   
    }           
    ?>  

我想念什么吗?

我已阅读: http://php.net/manual/zh/pdostatement.rowcount.php 但没有帮助

推荐答案

PDOStatement::rowCount()在某些数据库中不返回受 SELECT 语句影响的行数. 文档下面的代码使用SELECT COUNT(*)fetchColumn().还准备了语句和try& catch块可捕获异常.

PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement in some databases. Documentation The code below uses SELECT COUNT(*) and fetchColumn(). Also prepared statements and try & catch blocks to catch exceptions.

<?php
// Get parameters from URL
$id = $_GET["client"];
try {
    $db = new PDO('mysql:host=localhost;dbname=XXXX;charset=utf8', 'XXXX', 'XXXX');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // Prepare COUNT statement
    $stmt1 = $db->prepare("SELECT COUNT(*) FROM client WHERE client = ?");
     // Assign parameters
    $stmt1->bindParam(1,$id);
    $stmt1->execute();
    // Check the number of rows that match the SELECT statement 
    if($stmt1->fetchColumn() == 0) {
        echo "No Clients Found";
    }else{
        //echo "Clients Found";
        // Prepare Real statement
        $stmt2 = $db->prepare("SELECT * FROM client WHERE client = ?");
     // Assign parameters
        $stmt2->bindParam(1,$id);
        $stmt2->setFetchMode(PDO::FETCH_ASSOC);
        $stmt2->execute();
        while($row = $stmt2->fetch()) {
            //YOUR CODE HERE  FROM
             // Title
             echo '<div id="portfolio_detail">';
             //etc.etc TO
             echo '<div><img src="'."/client/".$row[client].'_3.png"/></div>';
             echo '</div>'; 
        }//End while
    }//End if else
 }//End try 
 catch(PDOException $e) {
    echo "I'm sorry I'm afraid you have an Error.  ". $e->getMessage() ;// Remove or modify after testing 
    file_put_contents('PDOErrors.txt',date('[Y-m-d H:i:s]').", myfile.php, ". $e->getMessage()."\r\n", FILE_APPEND);  
 }
//Close the connection
$db = null; 
?>

这篇关于PDO版本的mysql_num_rows($ result)== 0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆