简体   繁体   中英

Select count(*) from database

I have this SQL query which is working as it should when I run it into phpMyAdmin.

SELECT COUNT( * ) , LENGTH( Number ) AS Numbers
FROM  `history_2015-07-22` 
WHERE Number NOT LIKE  '123%'
OR LENGTH( Number ) <50
GROUP BY Numbers
ORDER BY TIME =  '2015-07-22 00:00:01' ASC 

I want now to make a simple php page where I want to display the query results on the browser but I can't figure out how to echo it exactly. So i've made this:

$result = $pdo->prepare("SELECT COUNT( * ) , LENGTH( Number ) AS Numbers
                         FROM  `history_2015-07-22` 
                         WHERE Number NOT LIKE  '123%'
                         OR LENGTH( Number ) <50
                         GROUP BY Numbers
                         ORDER BY TIME =  '2015-07-22 00:00:01' ASC ");
$result->execute();
foreach ($result as $Numbers)
{
    echo '<div class="container">
                '.$Numbers['COUNT(*)'].'
                '.$Numbers['LENGTH(Number)'].'
          </div>';
}

What I want to echo is Count and Length . I'm sure is something very simple what I miss but can't figure it out.

First, can you please explain what you're trying to do exactly with the SQL query?

From what I understand, you can try this:

$result = $pdo->prepare("SELECT COUNT( * ) AS ct_all, LENGTH( `Number` ) AS Numbers
                         FROM  `history_2015-07-22` 
                         WHERE `Number` NOT LIKE ('123%')
                         AND Numbers < 50
                         GROUP BY Numbers
                         ORDER BY `TIME` ASC");
$result->execute();
$results = $result->fetchAll();
foreach ($results as $row) {
    echo '<div class="container">';
    echo $row['ct_all'] . ' // ';
    echo $row['Numbers'];
    echo '</div>';
}
$result = $pdo->prepare("SELECT COUNT( * ) as cnts, LENGTH( Number ) AS num
                         FROM  `history_2015-07-22` 
                         WHERE Number NOT LIKE  '123%'
                         OR LENGTH( Number ) <50
                         GROUP BY num
                         ORDER BY TIME =  '2015-07-22 00:00:01' ASC ");
$result->execute();
foreach ($result as $Numbers)
{
    echo '<div class="container">
                '.$Numbers['cnts'].'
                '.$Numbers['num'].'
          </div>';
}

Here Have a Look I've pointed some issues.

$result = $pdo->prepare("SELECT COUNT( * ) AS ct_all, LENGTH( `Number` ) AS Numbers
                         FROM  `history_2015-07-22` 
                         WHERE `Number` NOT LIKE ('123%')
                         AND Numbers < 50
                         GROUP BY Numbers
                         ORDER BY `TIME` ASC");
    $result->execute();
   // the problem is.. 
   // you are trying to fetch $result but here $result is just executing 
   // you cannot retrive anything unless you didn't declare it 
   /*  here $result has nothing in it; is just executed first
    * declare it 
   */
     $result = $result->execute();
    /* Then Fetch it 
    And then You Can Use You Fetch Var with index to retrive data. 
    e.g */
    $allData = $result->fetchAll();
    foreach ($allData as $SingleData)
{
// here you must place indexes of your Query 
// e.g $SingleData['id'] or $SingleData[0]
        echo '<div class="container">
                    '.$SingleData['COUNT(*)'].'
                    '.$SingleData['LENGTH(Number)'].'
              </div>';
    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM