Archive for the ‘PHP’ Category

Choosing the right data type for the field of MySQL tables is always a task full of confusions. You have a number of data types available that can fulfill your needs. So its hard to decide which data type will fit the requirements. In this article I have discussed about the most used data types of MySQL which may help you in deciding about the exact data type to be used for your MySQL fields.


Numbers
Storing Whole Numbers:
If you’re storing whole numbers, use one of the integer types: TINYINT, SMALLINT, MEDIUMINT, INT, or BIGINT. These require 8, 16, 24, 32, and 64 bits of storage space, respectively. They can store values from -2(Nā€“1) to 2(Nā€“1)ā€“1, where N is the number of bits of storage space they use. If you do not want the negative values to be stored, use the UNSIGNED attribute with the field. It will double the storage space of the datatype. For example, a TINYINT UNSIGNED can store values ranging from 0 to 255 instead of from -128 to 127.

Storing Real Numbers:
Real numbers are the numbers with fractional part. MySQL provide three datatypes to store Real numbers. These datatypes are:

DECIMAL: The DECIMAL type is for storing exact fractional numbers. A DECIMAL number in MySQL 5.0 and newer can have up to 65 digits. DECIMAL type allows us to specify the maximum didits before and after the decimal point. This increases the space consumption in DECIMAL type. So DECIMAL type should be used only when you need mathematical data of high precision and you don’t care about the storage space.

FLOAT: Note that “Using FLOAT might give you some unexpected problems because all calculations in MySQL are done with double precision”.

DOUBLE: DOUBLEs are just like floats, except for the fact that they are twice as large. This allows for a greater accuracy. Both FLOAT and DOUBLE represent floating point numbers. A FLOAT is for single-precision, while a DOUBLE is for double-precision numbers. Floating-point types typically use less space than DECIMAL to store the same range of values. MySQL uses four bytes for single-precision values and eight bytes for double-precision values.

Strings
VARCHAR: VARCHAR is a variable length type. It requires less space than fixed length types (CHAR) because it uses only as much space as it needs. But it uses 1 or 2 extra bytes to store the length of the data. So VARCHAR(10) will use 11 bytes whereas VARCHAR(1000) will use 12 bytes of storage space.
Pros:
1. Less storage space.
2. Improved performance because of saved space.
Cons:
1. Updates are slow due to variable-length.
So you should use VARCHAR type when you do not know about the maximum length of the data in advance.

CHAR: CHAR is fixed-length and is useful if you want to store very short strings. When storing a CHAR value, MySQL removes any trailing spaces. It is good to use CHAR type if all the values are nearly the same length.

BLOB and TEXT: BLOB and TEXT are string data types designed to store large amounts of data as either binary or character strings, respectively. The only difference between the BLOB and TEXT families is that BLOB types store binary data with no collation or character set, but TEXT types have a character set and collation. These types should be used only if data to be stored is of very large length. Otehrwise avoid these types as it will make the queries slow and provide no indexing.

ENUM: ENUM store a predefined set of distinct string values. Each string is internally stored as an integer. In a simple select query, you will see the integer value as output instead of trhe string. ENUM should only be used if you are sure that no other values will be entered or removed from the list. Also there is a overhead of mapping the integer ID of ENUM values with the actual string representation which may degrade the performance in some cases.

Date and Time
DATETIME: DATETIME stores the date and time in YYYYMMDDHHMMSS format without considering the timezone. Is uses uses eight bytes of storage space.

TIMESTAMP: TIMESTAMP stores the date and time in the UNIX timestamp format i.e the number of seconds elapsed since midnight, January 1, 1970, Greenwich Mean Time (GMT). TIMESTAMP uses only four bytes of storage space. TIMESTAMP depends on the timezone. TIMESTAMP also provides the auto update property. So always try to use TIMESTAMP type than DATETIME.

$i = imagecreatefromjpeg("image.jpg");

for ($x=0;$x<imagesx($i);$x++) {
    for ($y=0;$y<imagesy($i);$y++) {
        $rgb = imagecolorat($i,$x,$y);
        $r   = ($rgb >> 16) & 0xFF;
        $g   = ($rgb >>  & 0xFF;
        $b   = $rgb & 0xFF;

        $rTotal += $r;
        $gTotal += $g;
        $bTotal += $b;
        $total++;
    }
}

$rAverage = round($rTotal/$total);
$gAverage = round($gTotal/$total);
$bAverage = round($bTotal/$total);
<?php

// Our custom error handler
function nettuts_error_handler($number, $message, $file, $line, $vars){
	$email = "
		<p>An error ($number) occurred on line
		<strong>$line</strong> and in the <strong>file: $file.</strong>
		<p> $message </p>";

	$email .= "<pre>" . print_r($vars, 1) . "</pre>";

	$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

	// Email the error to someone...
	error_log($email, 1, 'you@youremail.com', $headers);

	// Make sure that you decide how to respond to errors (on the user's side)
	// Either echo an error message, or kill the entire project. Up to you...
	// The code below ensures that we only "die" if the error was more than
	// just a NOTICE.
	if ( ($number !== E_NOTICE) && ($number < 2048) ) {
		die("There was an error. Please try again later.");
	}
}

// We should use our custom function to handle errors.
set_error_handler('nettuts_error_handler');

// Trigger an error... (var doesn't exist)
echo $somevarthatdoesnotexist;

Whois query using PHP in php

Posted: January 12, 2013 in PHP
Tags: ,
function whois_query($domain) {

    // fix the domain name:
    $domain = strtolower(trim($domain));
    $domain = preg_replace('/^http:\/\//i', '', $domain);
    $domain = preg_replace('/^www\./i', '', $domain);
    $domain = explode('/', $domain);
    $domain = trim($domain[0]);

    // split the TLD from domain name
    $_domain = explode('.', $domain);
    $lst = count($_domain)-1;
    $ext = $_domain[$lst];

    // You find resources and lists 
    // like these on wikipedia: 
    //
    // http://de.wikipedia.org/wiki/Whois
    //
    $servers = array(
        "biz" => "whois.neulevel.biz",
        "com" => "whois.internic.net",
        "us" => "whois.nic.us",
        "coop" => "whois.nic.coop",
        "info" => "whois.nic.info",
        "name" => "whois.nic.name",
        "net" => "whois.internic.net",
        "gov" => "whois.nic.gov",
        "edu" => "whois.internic.net",
        "mil" => "rs.internic.net",
        "int" => "whois.iana.org",
        "ac" => "whois.nic.ac",
        "ae" => "whois.uaenic.ae",
        "at" => "whois.ripe.net",
        "au" => "whois.aunic.net",
        "be" => "whois.dns.be",
        "bg" => "whois.ripe.net",
        "br" => "whois.registro.br",
        "bz" => "whois.belizenic.bz",
        "ca" => "whois.cira.ca",
        "cc" => "whois.nic.cc",
        "ch" => "whois.nic.ch",
        "cl" => "whois.nic.cl",
        "cn" => "whois.cnnic.net.cn",
        "cz" => "whois.nic.cz",
        "de" => "whois.nic.de",
        "fr" => "whois.nic.fr",
        "hu" => "whois.nic.hu",
        "ie" => "whois.domainregistry.ie",
        "il" => "whois.isoc.org.il",
        "in" => "whois.ncst.ernet.in",
        "ir" => "whois.nic.ir",
        "mc" => "whois.ripe.net",
        "to" => "whois.tonic.to",
        "tv" => "whois.tv",
        "ru" => "whois.ripn.net",
        "org" => "whois.pir.org",
        "aero" => "whois.information.aero",
        "nl" => "whois.domain-registry.nl"
    );

    if (!isset($servers[$ext])){
        die('Error: No matching nic server found!');
    }

    $nic_server = $servers[$ext];

    $output = '';

    // connect to whois server:
    if ($conn = fsockopen ($nic_server, 43)) {
        fputs($conn, $domain."\r\n");
        while(!feof($conn)) {
            $output .= fgets($conn,128);
        }
        fclose($conn);
    }
    else { die('Error: Could not connect to ' . $nic_server . '!'); }

    return $output;
}
function fb_fan_count($facebook_name){
    // Example: https://graph.facebook.com/digimantra
    $data = json_decode(file_get_contents("https://graph.facebook.com/".$facebook_name));
    echo $data->likes;
}