2

This MYSQL statement is functioning in MYSQL.

SELECT SUM(TABLE_ROWS) 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_SCHEMA = 'sample'

But when I put into my PHP code it does not funtion. What am I gonna do? The image below encircled with red is what I want to output in my page. Thank you.

enter image description here

6
  • What does it do when you run it? That query is fine. How do you execute it? Commented Apr 21, 2016 at 3:50
  • 1
    Are the credentials you use in phpmyadmin the same as for php? You may be using incorrect permissions Commented Apr 21, 2016 at 3:51
  • @Hanky yeah totally fine. I execute it as it is <?php $stmt = $conn->query('SELECT SUM(TABLE_ROWS) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = sample '); $row_count = $stmt->rowCount(); echo $row_count; ?> Commented Apr 21, 2016 at 3:53
  • @Terminus that is Im thinking haha :D Commented Apr 21, 2016 at 3:54
  • 1
    rowCount() ? no this will return only one row. So you have to fetch the result and see what SUM(TABLE_ROWS) contains Commented Apr 21, 2016 at 3:54

1 Answer 1

3

Your code looks fine, but in comment you wrote that where clause in your query looks like:
WHERE TABLE_SCHEMA = sample, but it's incorrect because sample must be passed like 'string'.

Here my code, and it works:

<?php

$dbh = new PDO('mysql:host=127.0.0.1;dbname=myDB', 'myUser', 'myPass');
$sth = $dbh->prepare("
    SELECT SUM(TABLE_ROWS) AS totalRowsCount
    FROM INFORMATION_SCHEMA.TABLES 
    WHERE TABLE_SCHEMA = 'myDB'
");
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
var_export($result);

and as result will be:

[0 => ['totalRowsCount' => '125']]
Sign up to request clarification or add additional context in comments.

3 Comments

i got it alreay haha. I almost having 4-5 hours of finding an answer to this matter. so our time is already 1:55pm PHIL time. anyways fetchAll is kinda for array therefore we'll be using foreach($result[0] as $child) { echo $child . "\n"; } to output in a nice way. **output now will be:** 125 Thanks Vladimir. and all the guys there. :)
You also can use $result = $sth->fetchColumn();.
You may add a note that this code would work only when emulation mode is turned on

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.