Solved

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

Posted on 2014-12-23
5
242 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
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

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

Accepted Solution

by:
AngryBinary earned 500 total points
Comment Utility
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
Comment Utility
Thank You!!!!
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
Comment Utility
Gary, I will ask another related question.   Coming shortly.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now