Throw Exception not working properly

I have been using this partial code for quite some time

if(!$result){
             throw new Exception($query);

with this that connects to a database table

$result = $this->db->connection->query($query);

I am trying to insert data into a table i.e. usernames, password and email

It was working for a month ago, now bang, it doesn’t seem to work
This is the MySQL query

$query = "SELECT COUNT(*) AS count FROM adminuser WHERE user='{$_name}'";

I am literally pulling my hair out over this, all my other database queries work except for this code which is baffling me to no end

An aggregate function (such as COUNT()) always returns a value, even if the WHERE or GROUP BY clause does not result in a match.

1 Like

Really? I thought the whole point of the Throw Exception was to see if there was a table and/or connection being made to the database, if not, it should throw an exception error

you can use an exception for that, but you can use it for any other error handling as well. and as the code indicates, it is used for checking a falsy return value of the query (which may be a primitive or an object, depending on the used implementation).

anyways, neither PDO nor mysqli let you handle connection errors like that. PDO always throws its own exception and mysqli always returns a connection object (unless you make mysqli throw its own exception) i.e. you have to explicitly test for connection errors.

1 Like

Are there any others that can help?

I think your issue is further down the line not with the query.

1 Like

I best solution for that is smart debugging like xdebug.

Any issue that can be recreated can be solved using smart debugging.

1 Like

Where can I get Java version 8 update 60? I think this is mucking up the script, it’s hard to explain, but ever since I’ve had this installed I’m sure u65 is the cause of the problem

EDIT: Ignore that bit

Oh my gosh, what a numpty I am

Was trying to INSERT INTO a column that didn’t exist in the table, thanks

that would have caused an (SQL) error. hence your error handling of SQL queries needs to be improved.

Hi, I’m glad you got it working. But you should never use string concatenation to build queries - you risk SQL injection this way. Please use prepared statements with bound variables as explained here.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.