How to perform MySQL query with IP2Proxy CodeIgniter 4 Library

IP2Proxy CodeIgniter 4 Library is fully integrated with supporting MySQL query in the latest version. This new feature enables the user to get the proxy information from the data table easily. In order to use the MySQL query, the IP2Proxy Database in CSV format needs to be downloaded and imported into the database. The IP2Proxy Database table creation code can be found at the IP2Location FAQs page.

Below, we are going to show you an example of how to perform a MySQL query with the IP2Proxy PX11 Database in the CodeIgniter 4.

  1. Create the IP2Proxy PX11 Database table by using the following code.
CREATE DATABASE ip2proxy;
USE ip2proxy;
CREATE TABLE `ip2proxy_px11`(
	`ip_from` INT(10) UNSIGNED,
	`ip_to` INT(10) UNSIGNED,
	`proxy_type` VARCHAR(3),
	`country_code` CHAR(2),
	`country_name` VARCHAR(64),
	`region_name` VARCHAR(128),
	`city_name` VARCHAR(128),
	`isp` VARCHAR(256),
	`domain` VARCHAR(128),
	`usage_type` VARCHAR(11),
	`asn` VARCHAR(6),
	`as` VARCHAR(256),
	`last_seen` INT(10),
	`threat` VARCHAR(128),
	`provider` VARCHAR(256),

	PRIMARY KEY (`ip_from`, `ip_to`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

2. Define the database details like database name, username and password in the app/Config/Database.php file.

public $default = [
	'DSN'      => '',
	'hostname' => 'localhost',
	'username' => 'YOUR_USERNAME',
	'password' => 'YOUR_PASSWORD',
	'database' => 'ip2proxy',
	'DBDriver' => 'MySQLi',
	'DBPrefix' => '',
	'pConnect' => false,
	'DBDebug'  => (ENVIRONMENT !== 'development'),
	'charset'  => 'utf8',
	'DBCollat' => 'utf8_general_ci',
	'swapPre'  => '',
	'encrypt'  => false,
	'compress' => false,
	'strictOn' => false,
	'failover' => [],
	'port'     => 3306,
];

3. Copy the following sample code into the app/Controllers/IP2Proxy_test.php file.

use App\Models\IP2Proxy_model;

define('IP2PROXY_DATABASE_TABLE', 'ip2proxy_px11');

class IP2Proxy_test extends BaseController {
    public function index() {
        // MySQL Query
        $db = model('IP2Proxy_model', false);
        $data = $db->lookup('1.0.241.135');
        echo '<pre>';
        print_r ($data);
        echo '</pre>';
        echo '<p>Country name for 1.0.241.135: ' . $data['country_name'] . '</p>';
    }
}
  1. You may run the sample code by using <your_domain>/index.php/ip2proxy_test.
  2. Done.

For the above configuration, the proxy information about the IP address “1.0.241.135” will be shown in the page. You may also display the data based on the field name that you defined. For instance, $data['region_name'], $data['city_name'] or $data['domain'].

Was this article helpful?

Related Articles