PHP PDO SQL Server Null结果导致异常

时间:2018-02-27 21:08:16

标签: php sql-server exception pdo

我有一个PDO类,因此我可以轻松地在数据库类型之间切换。

  //*** Function: createQuery, Purpose: Create the query object ***
  function createQuery($theQuery)
        {
        $theQuery = str_replace('`', "'", $theQuery);
        $this->query = $theQuery;
        $this->sth = $this->pdo->prepare($theQuery, array(PDO::ATTR_CURSOR 
        => PDO::CURSOR_FWDONLY));
        }

  //*** Function: query, Purpose: Execute the PDO ***                                                                    
  function query($paramArray = [], $column = null)
        {
        try
        {
        if(!empty($paramArray))
              foreach ($paramArray as $key => &$val)
                    { $this->sth->bindParam($key, $val);}
        $this->sth->execute();
        }
        catch(PDOException $e) { $this->error_catcher( $e->getMessage());}

        if($column != null)
              {
              $column = ($this->sth->columnCount() < $column) ? 0 : $column;
              return $this->sth->fetchAll( PDO::FETCH_COLUMN, $column );
              }
        else
              return $this->sth->fetchAll();
        }

这一切都很顺利。但是,有时,如果结果为NULL,则代码会抛出异常

这是构建菜单的典型查询:

 SELECT TOP (100) PERCENT caption, id
 FROM dbo.com_menuitems
 WHERE (suite = 'chrimbotu') AND (parentid = 0) AND (enabled = 1) AND (id IN
    ( 
      SELECT parentid AS id FROM dbo.com_menuitems AS com_menuitems_1
      WHERE (suite = 'chrimbotu') AND (perms <= - 1)GROUP BY parentid
      HAVING (parentid > 0))
    )
 ORDER BY parentid, vieworder, caption

我加载查询(从另一个脚本),如下所示:

  function getTopLevel($mySQL)
    {
    $this->_myConn->createQuery($mySQL);
    $result = $this->_myConn->query() or die (DumpSQL($mySQL).__FUNCTION__ .' Query failed.');
    $this->_topLevel = $result[0]['perms'];
    } 

如果我在数据库中运行没有'chrimbotu'的查询,SSMS会说[NULL,NULL]我的脚本会出现问题并引发有用的错误“查询失败”。

如果我将'chrimbotu'添加到数据库中,但给它一个9的perms,SSMS说[NULL,NULL]但我的脚本很高兴。

两个查询都返回NULL,但是比另一个查询更“空”?

FYI:0是最低权限,&lt; 0是“每个人”的东西

0 个答案:

没有答案