Skip to main content

Posts

Showing posts from December, 2011

php validate image type

You can validate image in php, this way: <?php     $img = "banner.gif"; // an image     $info = getimagesize($img);     print image_type_to_mime_type($info[2]); ?> The outout will generally be: image/jpeg or image/gif or image/png But if it is: application/octet-stream, then the file is not an image.

Interview questions

These are some of the php and mysql interview questions i faced: > difference between HAVING and WHERE clause in mysql > how can you interchange the genders of male and female in a mysql table having the column gender with values 'male' and 'female' (refer: http://stackoverflow.com/questions/8400617/mysql-replace-values-in-a-table ) > What is the use of singleton class in php

How to edit php.ini in the cpanel

It is often required to change some settings in the php configuration of our site. If you have access to the cpanel of your hosting provider, then the below steps can help you in modifying the settings in php.ini file..... (Note: these steps are specific to hostmonster) Step 1- Login into your cPanel. Step 2- Under the Software/Services category, click on the PHP Config icon. Step 3- In this screen, there is a section called Install Default php.ini. Under that section, please check mark the "IonCube" and the "SourceGuardian" check boxes (Many commercial php scripts need these turned on - if you think your script may not require the IonCube or SourceGuardian packages, it is okay to not check mark the boxes - but it will not hurt anything to check mark both boxes.). Step 4- Under the same section, click the "INSTALL PHP.INI MASTER FILE" button. Step 5- You have just successfully installed the "php.ini.default" file . But the next step is to r

Mysql queries (complex)

Scenario-1 : Using the count or sum with inner join query Table structures: videos: id, title, description, date_uploaded categories: id, category_name votes: id, videoid, vote, date (vote will range between 1 and 5) Query: SELECT v.id as vid, v.title, v.description, v.date_uploaded, c.category_name, v2.total, v2.count FROM videos v inner join categories c inner join (select videoid, sum(vote) as total, count(*) as count from votes group by videoid) v2 on v.category_id=c.id and v2.videoid=v.id and v.featured=1 and v.published=1 order by v.date_uploaded desc; Resultset: vid  title  description    date_uploaded            id    category_name   total  count 12   Ford   inauguration 2011-10-07 01:08:22    3    Inauguration        97     31 9     Nano   new car        2011-11-02 03:15:25    2    Automobile        120    38   From the above result set, we can get the percentage of votes for videos as: (total / count) * 20 Note: The above query assumes that you have a

php strip all tags

The below function can strip almost all tags from a string. function strip_all_tags($string) {     $string = preg_replace( '@<(script|style)[^>]*?>.*?@si', '', $string );     $string = strip_tags($string);     return trim($string); } $a = '<script type="text/javascript" src="jquery.js"></script> <div id="test" style="padding:5px; color:red;">Hello world</div>'; echo strip_all_tags($a); // outputs: Hello world