PHP & MySQL

Unable to find the wrapper “https”

Posted by admin on October 29, 2011 at 7:07 am

Today I had to put a simple script on one of my hosting server where a “Facebook Login” button was available for login. However when I had to run it I encountered the following error: Warning: file_get_contents() [function.file-get-contents]: Unable to find the wrapper “https” – did you forget to enable it when you configured PHP? [...]

$_FILES limitation when uploading files in PHP

Posted by admin on October 14, 2011 at 11:10 am

Along with the “upload_max_filesize” when uploading files with PHP there is another parameter named “max_file_uploads” witch prevent uploading more files that the value of this parameter. Default size of this parameter is 20 and is should be enough for more apps then I can count but today I had to repair a bug in a [...]

How to use call_user_func_array with a simple function and with class methods

Posted by admin on May 28, 2011 at 1:49 pm

In this post you will see a example of using call_user_func_array (from PHP) to call a simple function, a public static method from a class and a public method. First I define one function and one class: function add($p1, $p2){    return $p1 + $p2; }   class CSimpleMath{    function __construct($v1) {     [...]

“Progress bar” for long running cron scripts in PHP

Posted by admin on May 12, 2011 at 3:43 pm

Long running scripts can be a pain if you dont know how much will be running. If you need to see a progress and flush() or ob_flush() do not work becouse of browser cache you can implement an “email progress bar”. PHP example: $step = 0; $last_percent = 0; $total_rows = while(1) {    $step++; [...]

How to replace substring in MySQL table field

Posted by admin on March 11, 2011 at 2:30 pm

This replace will search in every row of the table where condition from WHERE is true and replace function will replace all instances in a string. ex. if you have text “hello world” and you want to replace the “o” letter with “*” this will will transform the text in “hell* w*rld” UPDATE `mytable` SET [...]

PHP: trim spaces in the middle of a string

Posted by admin on February 14, 2011 at 1:29 pm

When you let users input text in a textfield or input box iti is always a good ideea to use trim function to remove spaces form the begining or the end of the string. Today I needed a function to trim spaces from the middle of a string and replace with a single space. /* [...]

MySQL: Select “last month” records

Posted by admin on February 4, 2011 at 2:27 pm

In order to select last month records you have to get first dai of the last month and the last day of the first month. Remember that current month could be January and the last month will in another year. Finding first day of the last month: DATE_FORMAT(CURRENT_DATE – INTERVAL 1 MONTH, "%Y-%m-01") Finding the [...]

MySQL: select “this month” records

Posted by admin on February 4, 2011 at 2:20 pm

Where “date” column is a Date type in “my_table” SELECT * FROM `my_table` WHERE YEAR(`DATE`) = YEAR(CURDATE()) AND MONTH(`DATE`) = MONTH(CURDATE())

MySQL: Select last 7 days records

Posted by admin on February 4, 2011 at 2:14 pm

For this example I use this table model: ID Name Date 1 Jon Dow 2011-01-10 2 Jane Dow 2011-01-10 … … … SELECT * FROM `my_table` WHERE `DATE` BETWEEN DATE_ADD(CURDATE(), INTERVAL -5 DAY) AND CURDATE()

MySQL: Select today, yesterday or day before yesterday records

Posted by admin on January 10, 2011 at 11:29 am

Usualy when you work with records where a date or timestamp field is present you need to select records from: today records yesterday records day before yesterday records Lets say you have a table (my_table) like this: ID Name Date 1 Jon Dow 2011-01-10 2 Jane Dow 2011-01-10 … … … where `Date` is date [...]