php select from table where condition
Posted: Mon Feb 13, 2017 12:30 pm
Now that I have manually created my databases and I have stuffed some data into them
given i have the IP address of the user, I wanted to select the country from the table where the ip is found
this reference is the whole enchilada which is more than need
SELECT [COUNTRY NAME COLUMN], [REGION NAME COLUMN], [CITY NAME COLUMN], [LATITUDE COLUMN], [LONGITUDE COLUMN], [ZIP CODE COLUMN], [TIME ZONE COLUMN], [ISP COLUMN], [DOMAIN NAME COLUMN], [NETSPEED COLUMN], [IDD CODE COLUMN], [AREA CODE COLUMN], [WEATHER STATION CODE COLUMN], [WEATHER STATION NAME COLUMN], [MCC COLUMN], [MNC COLUMN], [BRAND NAME COLUMN]
FROM [IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE TABLE]
WHERE [SEARCH IP NO] BETWEEN [IP FROM COLUMN] AND [IP TO COLUMN]
my code is incomplete, which is why I need to suggestions
the idea was to get a 2 letter country code I could feed to a switch statement, the country code is a 2 letter result, there is also another field with the full name that is not important for my present needs
given i have the IP address of the user, I wanted to select the country from the table where the ip is found
this reference is the whole enchilada which is more than need
SELECT [COUNTRY NAME COLUMN], [REGION NAME COLUMN], [CITY NAME COLUMN], [LATITUDE COLUMN], [LONGITUDE COLUMN], [ZIP CODE COLUMN], [TIME ZONE COLUMN], [ISP COLUMN], [DOMAIN NAME COLUMN], [NETSPEED COLUMN], [IDD CODE COLUMN], [AREA CODE COLUMN], [WEATHER STATION CODE COLUMN], [WEATHER STATION NAME COLUMN], [MCC COLUMN], [MNC COLUMN], [BRAND NAME COLUMN]
FROM [IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE TABLE]
WHERE [SEARCH IP NO] BETWEEN [IP FROM COLUMN] AND [IP TO COLUMN]
my code is incomplete, which is why I need to suggestions
Code: Select all
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "ip2location";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT country_code FROM ip2location_db1 WHERE " + $ip + " BETWEEN ip_from AND ip_to";
$result = $conn->query($sql);
?>