Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

8/01/2016

PHPMyAdmin -- Upgraded to PHP 7, PHPMyAdmin Stopped Exporting

I'm Having a Problem with the export.php File


I don't know if this is a bug in PHPMyAdmin or not,
But one line in the export.php where it says "end of fake loop", there is a break, that has a double exit, however, the program is crashing at this point.
I changed it from "break 2" to "break" and it is working again.


 // now export the triggers (needs to be done after the data because
        // triggers can modify already imported tables)
        if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
            || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
        ) {

            if (! $export_plugin->exportStructure(
                $db, $table, $crlf, $err_url,
                'triggers', $export_type,
                $do_relation, $do_comments, $do_mime, $do_dates
            )) {
                break 2;
            }

        }

2/19/2015

PHP: Special Character Encoding & those awful Black Diamonds with Questin Marks or Blank Squares

So, you get a weird character in your web output that might look like this:

   The nation�s first extraterrestrial governor

You know it's a special character -- maybe a angled apostrophe or curly quote, maybe some type of an accented character.

This is likely caused by a problem with two different types of character encoding.
The above text is a headline from a WordPress database that I have.

The WP database says it is encoded as "utf8_general_ci"
The head of my HTML5 web page specifies this:
"<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />"

So, why doesn't it come out correctly? I don't know.

However, there is a PHP function that you can use to test the character encoding of your string:

   echo mb_detect_encoding ($testHead, 'UTF-8,Windows-1252,ASCII,ISO-8859-1,ISO-8859-15');

The string being tested is $testHead.
The comma-separated parameter is a list of character sets to test on the string, and they are in a specific order.

I had an array of test headlines, and most of the output was "UTF-8", however, the test headlines that had the mystery characters said the encoding was "ISO-8859-1".

Solution #1 (WordPress specific):


Don't pull the post titles directly from  the database.
Instead, use:
    get_the_title($myPostID)

Solution #2:

Check your database structure. The way data has been saved to your database may have been incorrect. 

If you have PhpMyAdmin or know some other way to view the structure of your database, make sure the fields of your "collation" are "utf8-geneal-ci". With PhpMyAdmin, you can use drop-down menus to change the collation. Important! -- Make sure you have a backup of your database before doing this. In my case, the default database table setup was "latin1-swedish-ci" which was fine back in the days when "ISO-8859-1" was the default encoding. Of course, your situation could be different. This is just a suggestion.

After you have changed you database structure, you may need to implement changes with a command to update the database. In PhpMyAdmin, you can select a box next to the table name, and then you could select "Repair Table" to make the changes stick.

Solution #3:


There are several PHP function that will translate a string from "ISO-8859-1" to "UTF-8", but it's all very confusing because the results may not be what you are expecting. The characters might disappear, the entire string might disappear, or the oddball characters might not change at all. Here's a couple of examples:

     utf8_encode($myTestString);
     iconv("ISO-8859-1", "UTF-8", $myTestString);

-----

The best solution is probably to have the database input be saved in your database as UTF-8 to begin with. Then when you retrieve the data, you should be able to use the UTF-8 charset without translation in your web page. The web page has to have UTF-8 encoding specified.

If you do any of that wrong,

12/20/2009

What is Overloading Functions? How to Overload Functions in PHP?

Imagine if this is your objective:
  • Go to the grocery store.
You'll think of some way to get to the store, right?
Now, I'll give you the same objective, but with additional information.
  • Go to the grocery store. Use your feet.
  • Go to the grocery store. Use your car.
  • Go to the grocery store. Use a taxi cab.
  • Go to the grocery store. Least expensive method.
  • Go to the grocery store. Fastest method.
  • Go to the grocery store. There won't be any parking when you get there.
So, your objective remains the same, but the actions you perform to reach the objective changes depending on the parameters I'm giving you. Overloading a function is much like this.

With Function Overloading, there is one name for the function, but different types of parameters are passed to the function, and as a result, there are often multiple function definitions with the same name. These separate, but same-named function definitions are typically meant to produce similar results. Each definition computes the result based on the type of arguments it receives. Values passed to the functions could, for example, be either characters or numbers, or perhaps you might have 2 values, but the function is normally waiting to take 3.

Here's another example, we could have written our own function that returns a UNIX epoch timestamp (number of seconds from 1/1/1970 to the specified date: 1262322000 = Jan 1, 2010). These function calls are all legitimate with function overloading:
  • getDate();
  • getDate(2010);
  • getDate(2010, "Mar");
  • getDate(2010, "Mar", 13);

They all are designed to do the same thing, but because the number of parameters being sent can vary, the calculations must be handled differently. And without overloading, the program might crash if it does not receive all 3 values. In some languages, like C++ and JAVA, you would write different function headers and bodies that would accommodate the different numbers of parameters sent to the functions. The one with no parameters becomes the default method. If the number of parameters or type of data being passed to the function does not match any of the possibilities, then an error occurs.

Overloading Functions with PHP


PHP does not appear to support my definition of overloading. Instead the PHP manual talks about ways to compensate for calls to methods that don't exist in an object, or to create member variable value "setters" and "getters" dynamically.

Huh? I know -- a bit advanced and confusing. But PHP does have a useful way to accept different numbers of arguments passed to a function.

And it's easy, too. The function head can be written using default values.

PHP Example


Normally a basic function is written like this:
getDate() {
  // DO SOME CALCULATIONS
  return $someValue;
}


If your are passing, values to the function, you normally write it like this:
getDate ($year, $month, $day) {
  // DO SOME CALCULATIONS
  return $someValue;
}
That assumes you have all three pieces of data ready to go. But what if you have only the year, or just the year or month?

You could still get some kind of response back. How?

Assign default values to the possible variables that might be passed to the function:

getDate($year = 0, $month = "Jan", $day = 1) {
  // BODY OF FUNCTION THAT CAN HANDLE DIFFERENT VALUES
  // PASSED TO IT USING
  // IF STATEMENTS OR SWITCH STATEMENTS
  return $someValue;
}
Using default values does not overwrite the values that are passed to the function, rather it provides a backup in case no value has been passed by the function call. 

So, some example legal function calls could be:
getDate (); // year=0; month="Jan", day=1
getDate (2010); // year=2010; month="Jan", day=1
getDate (2010,"Aug"); // year=2010; month="Aug", day=1
getDate(2010,"Aug",25); // year=2010; month="Aug", day=25

9/25/2009

Ubuntu: PHP mCrypt Installation

mCrypt is an encryption program used on Linux systems.

To install mCrypt for PHP5 on your Ubuntu Linux system:
sudo apt-get install php5-mcrypt
Then restart Apache with:
sudo /etc/init.d/apache2 restart
I needed to do this so that  phpMyAdmin would have mcrypt available.

For more info about mcrypt, visit:

Official site
http://mcrypt.hellug.gr

PHP usage of mCrypt
http://www.php.net/manual/en/book.mcrypt.php