English 中文(简体)
如何在PHP上获取count(*)?
原标题:How to get count(*) on PHP?
  • 时间:2010-10-14 09:36:07
  •  标签:
  • php

我有以下MySQL的输出。

Array
(
[0] => stdClass Object
    (
        [id] => 19
        [date] => 2010-10-04 11:00:00
        [course] => Yoga
        [course_id] => 19
        [count(*)] => 2
        [capacity] => 20
    )

[1] => stdClass Object
    (
        [id] => 20
        [date] => 2010-10-04 13:00:00
        [course] => Spin
        [course_id] => 20
        [count(*)] => 1
        [capacity] => 24
    )
...
...

我可以获取日期、课程等,但我不知道如何使用PHP获取count(*)。

foreach ($bookingnum as $key => $list){
    echo "<tr valign= top >
";
    echo "<td align= center >".$list->date."</td>
";
            echo "<td align= center >".$list->course."</td>
";
          //  echo "<td align= center >".$list->count(*)."</td>
";
         // this is wrong. how to get count(*)?? 
 ....
 ...
问题回答

将基本查询修改为

SELECT COUNT(*) AS `total` ...

那么您可以将其用作

$list->total

哈,奇怪的情况你在那里。。。好吧,由于count(*)显然不是一个有效的属性名称,您可以做3件事:

  1. use $list->{"count(*)"}
  2. change the mysql query to count(*) as count and then use $list->count
  3. change request mode so you get an array as result instead of an stdClass Object
$total = count($bookingnum);
foreach ($bookingnum as $key => $list){
    echo "<tr valign= top >
";
    echo "<td align= center >".$list->date."</td>
";
    echo "<td align= center >".$list->course."</td>
";
    echo "<td align= center >".$total."</td>
";
}

哎呀!我的错,错误的回答,当我看到法布里克对他回答+1时,我抓住了要点

显然,您可以获得具有奇怪名称的属性,如$list->;{count(*)}。但理想情况下,您会希望更改查询。给计数一个别名,如<code>SELECT。。。计数(*)cnt,因为使用它会更容易。然后您可以获得$list->;cnt,而不是采用奇怪的语法。

Edit: If you are doing a pagination and want to figure out the total number of records available, you might want to adjust your query with SELECT SQL_CALC_FOUND_ROWS * FROM .. and after your main query is sent , send another query

SELECT FOUND_ROWS() as total_records

但是,请记住SQL_CALC_FOUND_ROWS具有性能影响。

Edit: Just realized i just got the question wrong, really silly answer.. taking the first part back :)





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签