Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-12-23
5
Medium Priority
?
264 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

705 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