August 25th, 2016, 12:51 PM
Change a specific query field within echo based on it's value?
Hi, I’m needing my below query results to display “No” when ‘issue’ is a 0, and “Yes” when ‘issue’ is a 1.
Returns something like:
$sql = "SELECT client_id,issue,comments FROM clients”;
13|1|there was an issue
13|Yes|there was an issue
Is that possible without changing the values within the database?
August 25th, 2016, 01:14 PM
Certainly. Simply loop through the resulting rows from your query and change '0' to 'No' and '1' to 'Yes' in that result array. Nothing will change the DB without issuing an 'UPDATE' query.
There are 10 kinds of people in the world. Those that understand binary and those that don't.
August 26th, 2016, 03:32 AM
You can also have a look at the CASE (WHEN) statement to add the required output directly in the SQL.
Comments on this post
August 26th, 2016, 03:08 PM
Ah! That works better for what I'm needing since it's within the query. Couldn't quite get the loop through to work as needed, but was able to with the CASE statement. Cool, thanks for pointing that out!
Originally Posted by MrFujin
August 26th, 2016, 08:25 PM
Just to throw it out there, to use within the while() loop or such...
Wrote the 3 lines just for favored formats and such...
$response = $value == 1 ? 'There was an issue.' : 'No comment.';
$response = ($value == 1) ? 'There was an issue.' : 'No comment.';
$response = (($value == 1) ? 'There was an issue.' : 'No comment.');
$newVar = ($dbVar == 1) ? 'IF TRUE' : 'IF FALSE';
He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.