Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9192167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:51:29+00:00 2026-06-17T20:51:29+00:00

Possible Duplicate: Alternative for mysql_num_rows using PDO ^ I believe it isn’t the same

  • 0

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.


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>
    }   
    }           
    ?>  

Am I missing something?

I’ve read: http://php.net/manual/en/pdostatement.rowcount.php but hasn’t helped

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:51:31+00:00Added an answer on June 17, 2026 at 8:51 pm

    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; 
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Jmeter alternative Other than JMeter, whether there are any open source tool
Possible Duplicate: How are SSL certificate server names resolved/Can I add alternative names using
Possible Duplicate: Alternative to itoa() for converting integer to string C++? How to convert
Possible Duplicate: C# - Is there a better alternative than this to ‘switch on
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: Is there an alternative to Dictionary/SortedList that allows duplicates? I am looking
Possible Duplicate: Why is $.browser deprecated - and what is a better alternative? I
Possible Duplicate: Using comparison operators in Scala’s pattern matching system For below method I
Possible Duplicate: PHP split alternative? I am a beginner in php, Basically I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.