简体   繁体   English

从数据库中选择count(*)

[英]Select count(*) from database

I have this SQL query which is working as it should when I run it into phpMyAdmin. 我有这个SQL查询,当它运行到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. 我现在想制作一个简单的php页面,我想在浏览器上显示查询结果,但我无法弄清楚如何完全回显它。 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 . 我想要回应的是CountLength 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? 首先,您能否解释一下您正在尝试使用SQL查询做什么?

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>';
    } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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