Question

I am working on a project with a friend, we are building our own login and registration system for our site which we are creating. I am questioning his skills at coding in PHP with this following code statement:

   try { 
      $stmt = $db->prepare($query); 
      $results = $stmt->execute($params); 
      }
   catch() { 
      trigger_error('The query has failed.');
      }

I know that the SQL Query which we are going to perform is going to work for logging in the user, that is not the issue here and the reason why that part of the code is not being displayed within the code block above.

This is the very first time which I have saw someone use trigger_error() with PDOExeception $error statement, which was how I was taught to code to begin with.

Should we continue our core login, registration, and all SQL statements this way by using a Try, Catch, and Trigger_Error? Should I change it over to PDOExeception $error?

Was it helpful?

Solution

Neither.

Trigger error makes not a slightest sense here, as uncaught Exception already an error. So, to catch an error only to throw an error is a tautology. Moreover, uncaught Exception contains indispensable stack trace while just ordinary error doesn't.

Neither echo with catch is required. this is but a calloused delusion of PHP folks.

Neither writing four additional lines of code for the every query execution makes no sense as well. I even wrote a dedicated article on the matter - so, I wouldn't repeat myself

So, just rewrite this code to

  $stmt = $db->prepare($query); 
  $results = $stmt->execute($params); 

which is all you actually need

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top