【问题标题】:Function name must be a string comparison函数名必须是字符串比较
【发布时间】:2015-07-20 19:49:35
【问题描述】:

我想打印我的表UDS中的最新记录。

使用以下代码,我可以比较两条最新记录...
如果最新记录高于上一个记录,则我用绿色打印该值,否则如果最新记录小于上一个记录,我用红色打印...否则,我用黑色打印它/p>

$mrkfStatement = $mrkfPDO->prepare("select * from uds order by id desc limit 2  ");
$mrkfStatement->execute($params);
$pays = $mrkfStatement->fetchAll(PDO::FETCH_ASSOC);

$color = 'black';
$element = $count($pays);
if ($element > 0) { // we must have got two records back from the query
    if ($pays[$element]['price'] < $pays[$element - 1]['price'])
        $color = 'red';
    elseif ($pays[$element]['price'] > $pays[$element - 1]['price'])
        $color = 'green';
}
echo "<tr><td>
                        <span style='color: $color'>"
 . $pays[$i]['price'] .
 "</span>
                    </td>
                  </tr>";

但我遇到了这个错误:

未定义变量:计数

函数名必须是字符串($element = $count($pays);)

【问题讨论】:

    标签: php


    【解决方案1】:

    $element = $count($pays); 的错字应该是$element = count($pays);

    【讨论】:

      【解决方案2】:

      应该是 count() 而不是 $count()

      所以试试这个:

      $element = count($pays);
      

      【讨论】:

      • 当修改为 count($pays) .. 给出错误 /// 未定义的偏移量:2
      【解决方案3】:

      使用rowCount()。获取总行数

      $element = $mrkfStatement->rowCount();
      if ($element > 0) {
      

      【讨论】:

      • 注意:未定义的偏移量:第 29 行 C:\wamp\www\curl\tests\usd.php 中的 2 /// if ($pays[$element]['price']
      • 注意:未定义的偏移量:C:\wamp\www\curl\tests\usd.php 第 36 行中的 2 ///// echo " " 。 $pays[$element]['price'] 。 " ";
      • 使用for循环从数组中检索数据
      【解决方案4】:

      $count($pays) 替换为count($pays)

      【讨论】:

        猜你喜欢
        • 2012-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-04
        • 1970-01-01
        • 1970-01-01
        • 2012-01-03
        • 1970-01-01
        相关资源
        最近更新 更多