![]() |
CI4 - > MySQLI Database -> query return result object even if error - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: CI4 - > MySQLI Database -> query return result object even if error (/showthread.php?tid=77006) |
CI4 - > MySQLI Database -> query return result object even if error - johannes - 07-11-2020 Good day I did a test to see the Database objects and how the handle invalid queries. The logic is that if the database query does not return a valid result that false is returned else the resource id is returned via the CI Database framework Database\BaseConnection->query BaseConnection->simpleQuery MySQLi\Connection->execute the problem is that if one enteres a invalid query for example select * from somewherethatdoesnotexist then instead of it returned FALSE as one would expect for the error will be mysqli_sql_exception: Table 'dominion_employees.somewhere' doesn't exist in /MySQLi/Connection.php:331 it returns (the mySQLI php lib that is) this object, that makes all code to handle invalid queries after it not work as the object is a actual object now and not FALSE. PHP Code: ( I geuss one should in the mySQLI driver part check PHP Code: [errno] => 1146 and if it is set, return false rather ? Hope it helps Johannes RE: CI4 - > MySQLI Database -> query return result object even if error - johannes - 07-11-2020 Note I am in production mode thus the DBDebug = false then , thus in theory it should return false based on the logic in mysqli driver PHP Code: try RE: CI4 - > MySQLI Database -> query return result object even if error - johannes - 07-11-2020 Sorry I know I am adding to this but want to mention that hasError does not help (https://codeigniter.com/user_guide/database/queries.html#query-basics) as it seems not of the drivers implement the setError function that is suppose to set the vars to know if there was a error or not RE: CI4 - > MySQLI Database -> query return result object even if error - johannes - 07-11-2020 Hi there. Okay I discovered a errors in my post. Firstly query returns a object based on the BaseConnection class that will return a BaseResult object basically always. Thus I have to check the object as discribed above to get a idea of something went wrong. Then secondly hasError only works on Query Builder not on normal direct query functions. Thus the reasons it does not get called. Thus one can ignore the statement that hasError dont work as I am not using query builder todo these query tests. Thank you RE: CI4 - > MySQLI Database -> query return result object even if error - John_Betong - 07-11-2020 @johannes, > I did a test to see the Database objects and how the handle invalid queries. The logic is that if the database query does not return a valid result that false is returned else the resource id is returned via the CI I have never liked the practise of functions returning two different result types. I hope this bad practise will soon have alternatives which only allow valid strict_type return values. Using type-casting ensures the return types are always the same and the function result must be checked for validity. RE: CI4 - > MySQLI Database -> query return result object even if error - johannes - 07-12-2020 (07-11-2020, 05:47 PM)John_Betong Wrote: @johannes, Good day @John_Betong , luckily as you can see in my last post I have realized that I was wrong on the return type as the Base class did the conversion to ensure that the type returned is valid return type that is not a mix match , thus this at least was addressed and explains why the result object was returned that I did not expect but now understand, it also explained like that in their documents (the first place I should have checked) |