dragon012100
2017-06-21 08:43
浏览 18
已采纳

即使mysql表中有值,SQL查询也不返回值

I got this function:

private function _loadColumnRights() {

            $db = Zend_Registry::get('db');

            // Create the select query
            $select = $db->select()
                        ->from('account_columns', array(
                            'accountId'
                        ))
                        ->where('accountId = ?', $this->getId()); 

            // Fetch the data
            $row = $db->fetchRow($select);
            if($row != null) {
                $this->setColumns($row['columns']);
            }
            else {
                $this->setColumns('');
            }
        }

I search for the accountID = '128'

My table looks like this:

accountId | columns

and at row with accountId = '128' I got a value like this 'orderdate,ref-corlido,item-no,partid,item-description,quantity-value,unit-of-measurement,acquisition-value-order-line,sales-value-order-line,markup,due-date,status,direct-delivery,out-of-scope,non-compliant,memo,request-document,documents'

If I in PHP manually execute this SQL statement:

SELECT * FROM `account_columns` WHERE `accountId` = '128'

Then I get result..

my Code also goes in to this if:

if($row != null) {
            $this->setColumns($row['columns']);
        }

But the value of $row['columns'] is empty.. If I perform a var_dump on my view on this getter then i get empty values

If I change $row['columns']) to :

$this->setColumns($row['accountId']);

then I get the 128 value.. But I don't get the column values.. How come?

图片转代码服务由CSDN问答提供 功能建议

我有这个函数:

  private function _loadColumnRights()  {
 
 $ db = Zend_Registry :: get('db'); 
 
 //创建选择查询
 $ select = $ db-> select()
  - > from('account_columns  ',array(
'accountId'
))
  - > where('accountId =?',$ this-> getId());  
 
 //获取数据
 $ row = $ db-> fetchRow($ select); 
 if($ row!= null){
 $ this-> setColumns($ row ['columns  ']); 
} 
其他{
 $ this-> setColumns(''); 
} 
} 
   
 
 

我搜索 accountID ='128'

我的表格如下所示:

accountId | 列

accountId ='128'的行我得到了一个像'orderdate,ref-corlido,item-no,partid,item-description的值 ,数量值,单元的测量,采集值阶线,销售值阶线,标记,到期日,状态,直接递送,外的范围,不符合规定的,备忘录 ,请求文档,文档'

如果我在PHP中手动执行此SQL语句:

  SELECT * FROM`count_columns` WHERE`  accountId` ='128'
   
 
 

然后我得到结果..

如果符合以下情况,我的代码也会进入:< / p>

  if($ row!= null){
 $ this-&gt; setColumns($ row ['columns']); 
} 
  <  / pre> 
 
 

$ row ['columns']的值为空..如果我在此getter上的视图上执行var_dump,那么我得到空值

如果我将 $ row ['columns'] 更改为:

  $ this-&gt; setColumns(  $ row ['accountId']); 
   
 
 

然后我得到128值.. 但是我没有得到列值 ..怎么来的?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douye2020 2017-06-21 20:51
    已采纳

    Because you only ask for one column in the result row. :)

    You only as k for accountId...

        private function _loadColumnRights() {
    
            $db = Zend_Registry::get('db');
    
            // Create the select query
            $select = $db->select()
    
                        // THERE IS YOU ERROR :)
                        // THE SECOND PARAMETER IS AN ARRAY OF RESULT COLUMNS
                        ->from('account_columns', array(
                            //'accountId',  <-- Your error :)
                            '*' // use this if you want all columns back
                            //'columns' // or add this column name
                        ))
    
    
                        ->where('accountId = ?', $this->getId()); 
    
            // Fetch the data
            $row = $db->fetchRow($select);
            if($row != null) {
                $this->setColumns($row['columns']);
            }
            else {
                $this->setColumns('');
            }
        }
    

    P.S.: You really should use another name for the "columns" colmn... :) Look at the list of reserved words and keywords... don't use them for column or table names. :)

    https://dev.mysql.com/doc/refman/5.7/en/keywords.html

    Have a great day!

    Good luck!

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题