View Issue Details

IDProjectCategoryView StatusLast Update
0014336mantisbtbugtrackerpublic2012-06-16 06:34
Reportervincent_sels Assigned Todregad  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionno change required 
Product Version1.2.8 
Summary0014336: Don't throw error when using db_fetch_array on empty resultset
Description

When you execute a query which returns no results, an error is thrown when using db_fetch_array() on it, because the method tries to access the EOF property of an object which in that case is null.

By adding an extra check on the passed p_result to see whether it is null, you can safely use db_fetch_array for instance in while loops, without having to always manually check whether or not the result has rows. This makes code more readable and faster.

TagsNo tags attached.

Activities

dregad

dregad

2012-06-01 04:29

developer   ~0031942

See https://github.com/mantisbt/mantisbt/pull/42

grangeway

grangeway

2012-06-01 13:53

reporter   ~0031968

db_query/db_query_bound don't allow $p_result to be null - so this check shouldn't be needed as it's an extra function call...

grangeway

grangeway

2012-06-01 13:55

reporter   ~0031969

Dregad: assigned to you

dregad

dregad

2012-06-01 20:03

developer   ~0031970

@grangeway - it would have been better if you had written this comment in the pull request 4 months ago when rombert asked for your opinion...

@vincent_sels, can you comment on grangeway's note, and maybe provide an example case where your fix is actually useful/necessary ?

grangeway

grangeway

2012-06-02 14:33

reporter   ~0031983

I don't recall seeing a mail asking for an opinion... apologies if I missed it.

but db_query either returns an object or null on failure [ I'm not sure you'd actually hit the function after a failure due to the fact we tend to call trigger error

So basically to hit the fact you dont have an object, it looked at first glance that you'd need to call db_fetch_array() on something that didn't come from a return value db_query/db_query_bound - i.e. that would be a bug elsewhere

vincent_sels

vincent_sels

2012-06-04 05:18

reporter   ~0031997

Looks like grangeway is totally right, db_query_bound doesn't return null as the result set. Probably didn't initialize it somewhere when I thought I did. Feel free to close this and reject the pull request - although I don't think the extra check can do much harm.

dregad

dregad

2012-06-04 07:29

developer   ~0031998

Thanks for your feedback. Commit has been reverted.

Related Changesets

MantisBT: master 33170d2d

2012-02-07 07:49

vincent_sels


Details Diff
Don't throw error when using db_fetch_array on empty resultset

When you execute a query which returns no results, an error is thrown
when using db_fetch_array() on it, because the method tries to access
the EOF property of an object which in that case is null.

By adding an extra check on the passed p_result to see whether it is
null, you can safely use db_fetch_array for instance in while loops,
without having to always manually check whether or not the result has
rows. This makes code more readable and faster.

Fixes 0014336

Signed-off-by: Damien Regad <damien.regad@merckgroup.com>
Affected Issues
0014336
mod - core/database_api.php Diff File

MantisBT: master-1.2.x 9b1a282b

2012-02-07 07:49

vincent_sels


Details Diff
Don't throw error when using db_fetch_array on empty resultset

When you execute a query which returns no results, an error is thrown
when using db_fetch_array() on it, because the method tries to access
the EOF property of an object which in that case is null.

By adding an extra check on the passed p_result to see whether it is
null, you can safely use db_fetch_array for instance in while loops,
without having to always manually check whether or not the result has
rows. This makes code more readable and faster.

Fixes 0014336

Signed-off-by: Damien Regad <damien.regad@merckgroup.com>
Affected Issues
0014336
mod - core/database_api.php Diff File

MantisBT: master-1.2.x 7b087923

2012-06-04 00:13

dregad


Details Diff
Revert "Don't throw error when using db_fetch_array on empty resultset"

As per grangeway's comment and confirmed by vincent_sels (original
issue's reporter), this change is actually not necessary.

This reverts commit 9b1a282b239dea4095863ed71ac9e129592522a2.

Affects 0014336
Affected Issues
0014336
mod - core/database_api.php Diff File

MantisBT: master a12d6b7c

2012-06-04 00:26

dregad


Details Diff
Revert "Don't throw error when using db_fetch_array on empty resultset"

As per grangeway's comment and confirmed by vincent_sels (original
issue's reporter), this change is actually not necessary.

This reverts commit 33170d2d54643e63b1a60b021dcb0cbbb45e4157.

Affects 0014336
Affected Issues
0014336
mod - core/database_api.php Diff File