Archive for the ‘php’ Category

How To Show JSON Array Only In Recess PHP Controller

By default, Recess PHPs’s JsonView class responds with the properties of your controller. So even if you create a custom route that outputs only an array, a default row of the controller’s model will also be included with null values.

You can override this using the OKResponse class. You can do it like this.

PHP MySQL Result json_encode Includes Value And Its Column Index As Key

While this is not really an error, extra data that is not used should not be included in any output as it will only add to the extra bytes.

When you look at your JSON output, you realized the column names and values also have extra index column numbers and the associated values included.

To remove this, simply add an option in the PDO when you instantiate a database connection in PHP like this:

That should fix the problem. When you use json_encode(), there will be no more duplicate values with the corresponding column index number as key.

Which Is Better In PHP: PDO Or MySQLi?

I am not sure if this is a matter of one’s preference but I believe from my experience PDO provides more coding flexibility that MySQLi.

Here is an example so you will better understand what I mean.

Now, if that were to be MySQLi, I would be doing it like this:

Notice the difference? I had to call if conditions twice. If my query is very long, then my code would also be long.

Plus, there is also the issue of mysqli_escape_string(). With PDO, you do not have to call one since it calls that function automatically when you bind values to the parameters.

When I started using prepared statements in MySQL because of the SQL injection issue, this is the main reason why I opted to use PDO rather than MySQLi.

Related Posts Plugin for WordPress, Blogger...