I want to fetch one single value from my database. Therefore I use the following code:
我想从我的数据库中获取一个值。因此我使用以下代码:
$qb->select('u.userId')
->from('user', 'u')
->orderBy('u.id', 'DESC')
->setMaxResults(1);
$query = $qb->getQuery();
$result = $query->getSingleResult();
The code works fine if there is at least one row in that table. But if the table is empty it throws an exception. How can I intercept this exception?
如果该表中至少有一行,则代码可以正常工作。但是如果表是空的,它会抛出异常。我如何拦截此异常?
I tried stuff like if($result->isEmpty()
, $result === null
or empty($result)
but my exception seems to be thrown at line no. 3 in the example above, so I can't intercept it afterwards.
我试过像if($ result-> isEmpty(),$ result === null或empty($ result)这样的东西但我的异常似乎是在上面的例子中的第3行抛出,所以我无法拦截它然后。
1 个解决方案
#1
1
This should work.
这应该工作。
IF EXISTS(<query>)
SELECT 'not empty'
ELSE
SELECT 'empty'
#1
1
This should work.
这应该工作。
IF EXISTS(<query>)
SELECT 'not empty'
ELSE
SELECT 'empty'