0

Here is an example of what I am trying to understand:

return (@mysql_query($sql)) ? true:false;

I want to return either true or false if this line is executed successfully:

$this->sql->execute();

Can someone explain me how this if like thing works and how can I use it with my line?

1 Answer 1

1

This statement executes the mysql_query() call and if it was successful, returns the boolean TRUE from the function in which it resides. If the SQL query fails, it returns boolean FALSE.

Whether or not you can use this construct depends on if you expect your execute() call to return a result set (in a SELECT statement) or if it is used for INSERT/UPDATE/DELETE. If you attempt this with a SELECT statement, you would not be able to fetch your result rows with this statement.

If it is only for INSERT/UPDATE/DELETE, you may be able to do

return $this->sql->execute() ? true : false;

However, we still can't be sure this will work because you have not posted the contents of the class from which execute() is called.

The ternary operator is used to assign or return one value or another based on the result of a condition.

$returned_or_assigned_value = condition ? true_return_val : false_return_val;

Ternary operations are very commonly used to assign one of two values to a variable. Using them in a return statement is a similar operation, if you think of the variable assignee as the function's return.

Sign up to request clarification or add additional context in comments.

2 Comments

In case of SELECT statement you will get resource object that will be correspond TRUE value. But you will not be able to check does SELECT return values or no, just SQL syntax.
It's Doctrine ORM's query execute() method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.