Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

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

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
lawrence_dev
Asked:
lawrence_dev
  • 2
  • 2
1 Solution
 
AngryBinaryCommented:
Typo. Try "conn->exec();"
0
 
AngryBinaryCommented:
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
 
lawrence_devAuthor Commented:
Thank You!!!!
0
 
GaryCommented:
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
 
lawrence_devAuthor Commented:
Gary, I will ask another related question.   Coming shortly.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now