PHP trim functions: trim, rtrim, and ltrim

String variables are common in any programming language. You are likely going to use or manipulate a string at some point while programming. Sometimes, string variables are not at all perfect. Many of them may even have characters that need to be removed. Targeting specific locations of a string, PHP provides 3 string functions we can use to remove unwanted characters from a string variable: trim(), ltrim() and rtrim(). Continue reading to see examples of each function…

[Keep reading for more SQL database and PHP/Python-centric content >>>]

PHP empty() function use with MySQL NULL

PHP provides a handy function, empty(), that is used to determine whether a variable is empty. Perhaps that is a bit confusing to someone unfamiliar with the empty() function and I can see how. In this blog post, I will cover: what empty() means in PHP, what the empty() function does, and a use case pairing up empty() with the PHP ternary operator conditional construct. Both used in combination with the MySQL NULL value. Continue reading and see examples of empty()

[Keep reading for more SQL database and PHP/Python-centric content >>>]

PHP MySQL BLOB PDF: Display in Browser

In Use MySQL BLOB column with PHP to store .pdf file, I covered an example of how to store a .pdf file in the actual database table using the MySQL BLOB datatype and PHP. Now that we have .pdf’s stored in the database, how do we display them in the browser? This blog post answers that exact question. Continue reading to see a working example using PHP…

[Keep reading for more SQL database and PHP/Python-centric content >>>]

Use MySQL BLOB column with PHP to store .pdf file

Like always I am sharing new things I learn here on my blog. I was recently working on a requirement for a LAMP stack web application reporting dashboard in which I needed to store – and eventually – retrieve a .pdf file. I have read in several places (this fantastic book is a great resource) that a viable option is storing images or documents (.pdf in this case) in the actual database table as opposed to on the server file system. MySQL has the BLOB datatype that can be used to store files such as .pdf, .jpg, .txt, and the like. In this blog post, I cover how I accomplished uploading and storing the actual .pdf file in a BLOB column in MySQL using PHP. Any corrections, tips, pointers, and recommendations for best practices are always welcome. We all learn as we go!!!

[Keep reading for more SQL database and PHP/Python-centric content >>>]

Sorting associative arrays in PHP with array_multisort() – New learning

If you write MySQL queries, at some point you are going to have to provide query results in a specific order. To impose any ordering in MySQL (this applies to SQL overall and is not directed only at MySQL), you have to use the ORDER BY clause. Without it, there is no guaranteed order. The database is free to send back query results in any order. As I learn PHP, I make it a point to explore both the MySQL side, along with the PHP side in regards to similar type tasks and the efficiency of each. So far in my PHP journey, I have found that arrays are used quite extensively. In this post, I’ll cover array_multisort() – one of many in-built PHP functions – used for sorting arrays. In the context of the example data for this post, the arrays are populated by an unordered MySQL query. Let’s see one example of how you can establish a sorting order in a PHP array, that is populated from an unordered MySQL query…

[Keep reading for more SQL database and Python/PHP-centric content >>>]