从mysql获取列值时获取未定义的索引 [英] Getting undefined index while fetching column value from mysql

查看:43
本文介绍了从mysql获取列值时获取未定义的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

$sql = 'select count(*) from match as count where match_status != :status';
      $query = $con->prepare($sql);
      $query->bindValue(':status',LOST,PDO::PARAM_INT);
      $query->execute();
      $row = $query->fetch(PDO::FETCH_ASSOC);

      if(!empty($row))
        $row_count = $row['count'];
      else
        $row_count = 0;

我收到注意:未定义索引:计数

怎么了?

推荐答案

$sql = 'select count(*) from match as count where match_status != :status';

在此查询中,as count 不会做任何事情,因为您不知道要重命名哪个列.
您需要将它直接放在原始列名之后,以便数据库知道它需要隐藏什么列名:

In this query, the as count won't do a thing, because you don't know what column you want to rename.
You need to place it directly after the original column name, so the database knows what column-name it needs to hide:

$sql = 'select count(*) as count from `match` where `match_status` != :status';

因为这个 $row['count'] 不起作用,因为你没有计数列,只有一个计数(*)列.

Because of this $row['count'] won't work, because you don't have a count-column, only a count(*) column.

注意:您使用的是 MySQL 保留字,即 match,在 MySQL 中需要特别注意.要么将其重命名为其他名称,要么在其周围使用勾号,以便正确转义.

N.B.: You're using a MySQL reserved word, being match and requires special attention in MySQL. Either rename it to something else, or use ticks around it, in order to escape it properly.

这篇关于从mysql获取列值时获取未定义的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆