Monday, October 19, 2015

Get country by mobile no

Here I am going to tell you how can you get country by mobile no
sometime you have the mobile no with country code


    mobile
   971525478965
   919844005522
   45712345678
 
I am going to through each number in the database and find the country code from
the mobile number display the country code and the country using php.
<?php
/** This is script for get country by mobile no from database ***/
/*
CREATE TABLE IF NOT EXISTS `countries` (
`idCountry` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`capital` varchar(100) NOT NULL,
`mobile` varchar(100) NOT NULL,
`code` varchar(50) NOT NULL,
`status` varchar(10) NOT NULL,
PRIMARY KEY (`idCountry`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=40 ;
*/
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'xxxxx';
$dbPassword = 'xxxxxx';
$dbDatabase = 'xxxxx';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");
$sql = "SELECT mobile FROM countries";
//print $sql;
$queryRes1 = mysql_query($sql);
while($rows=mysql_fetch_assoc($queryRes1))
{
$mobilecon[] = $rows['mobile'];
}
$phones = $mobilecon;
// get your list of country codes
$ccodes = array(
'1' => 'us',
'2' => 'uk',
'3' => 'de',
'45' => 'denmark',
'44' => 'fi',
'123' => 'no',
'971' =>'uae',
'91' =>'india',
'92' =>'pakistan'
);
krsort( $ccodes );
foreach( $phones as $pn )
{
foreach( $ccodes as $key=>$value )
{
if ( substr( $pn, 0, strlen( $key ) ) == $key )
{
// match
$country[$pn] = $value;
break;
}
}
}
print_r( $country );
?>

 
 
 
 If you  like then share 

7 comments:

  1. I'm reading your blog from past 1 week and I found it is very helpful for the beginners to php development.

    ReplyDelete
  2. I really impressed with your article, so great & useful knowledge you mentioned here.

    Website Development services

    ReplyDelete

How to Host a Laravel Project on Hostinger’s hPanel: A Step-by-Step Guide

How to Host a Laravel Project on Hostinger’s hPanel: A Step-by-Step Guide If you're looking to host a Laravel project on Hostinger’s hPa...