Solved

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

Posted on 2014-12-23
5
245 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CA single sign on 2 72
Google maps, choose location using pin 10 39
Session timeout 5 13
Configuring a checkbox in CSS and php 18 22
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

948 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

20 Experts available now in Live!

Get 1:1 Help Now