Solved

Transitioning to PDO/MySQL.  Need help  with counting rows in table

Posted on 2014-12-23
5
251 Views
Last Modified: 2014-12-23
I need to count rows in one table and compare to number of rows in another to make sure they are equal.

I have something incorrect to get data from the import table.   I am getting the following error:
Fatal error: Call to undefined method PDO::execute()

$query18 = "SELECT COUNT(*) FROM Download";
$conn->prepare($query18);       
$conn->execute();
$number_of_rows = $query18->fetchColumn();
echo $number_of_rows;

Open in new window

0
Comment
Question by:lawrence_dev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:AngryBinary
ID: 40515066
Typo. Try "conn->exec();"
0
 
LVL 13

Accepted Solution

by:
AngryBinary earned 500 total points
ID: 40515074
Actually, that's not going to give you the result you want. Try this instead:

$query18 = "SELECT COUNT(*) FROM Download";
$statement = $conn->prepare($query18);      
$statement->execute();
$number_of_rows = $statement->fetchColumn();
echo $number_of_rows;
0
 

Author Closing Comment

by:lawrence_dev
ID: 40515089
Thank You!!!!
0
 
LVL 58

Expert Comment

by:Gary
ID: 40515123
Invariably you are not doing a query just to get the number of rows, but wanting to know how many rows your query returned before working on the data - e.g. for pagination
So this would be the better way to do it.

$query18 = "SELECT * FROM Download";
$conn->query($query18);       

$totalrecords=$conn->query('SELECT FOUND_ROWS()')->fetch(PDO::FETCH_COLUMN); // Get result count

Open in new window

0
 

Author Comment

by:lawrence_dev
ID: 40515129
Gary, I will ask another related question.   Coming shortly.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question