CodeIgniter Forums
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:
(
    [connID] => mysqli Object
        
(
            [affected_rows] => -1
            
[client_info] => 5.5.66-MariaDB
            
[client_version] => 50566
            
[connect_errno] => 0
            
[connect_error] => 
            [errno] => 1146
            
[error] => Table 'somewhere' doesn't exist
            [error_list] => Array
                (
                    [0] => Array
                        (
                            [errno] => 1146
                            [sqlstate] => 42S02
                            [error] => Table '
somewhere' doesn't exist
                        
)

                )

            [field_count] => 0
            
[host_info] => Localhost via UNIX socket
            
[info] => 
            [insert_id] => 0
            
[server_info] => 5.6.41-84.1
            
[server_version] => 50641
            
[stat] => Uptime18262742  Threads6  Questions10888006908  Slow queries9126  Opens32322468  Flush tables1  Open tables16800  Queries per second avg596.186
            
[sqlstate] => 00000
            
[protocol_version] => 10
            
[thread_id] => 99238701
            
[warning_count] => 0
        
)

    [resultID] => 
    [resultArray] => Array
        (
        )

    [resultObject] => Array
        (
        )

    [customResultObject] => Array
        (
        )

    [currentRow] => 0
    
[numRows] => 
    [rowData] => 


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
 {
return 
$this->connID->query($this->prepQuery($sql));
}
catch (\
mysqli_sql_exception $e)
{
log_message('error'$e);
if (
$this->DBDebug)
{
throw 
$e;
}
}
return 
false



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,
> 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.

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)