theITvideos Posted September 7, 2010 Share Posted September 7, 2010 I am using a script that gets connected to PHP table and returns the visitors country name from: Database MyProjectA Table: ip2country and inside the script it uses the mysql_connect() function and it works fine BUT the code used in other pages to get data from other tables does not work. I mean, I have a combo box for example, that populates data from: Database MyProjectA Table: mybookings But it doesn't populate as long as the code for the 'user's country script' is active. The moment I comment out the 'user's country script', the combo box works. Perhaps I am using 2 database pooling (or connection pooling) so what I did, I closed the previous connection after I get the visitor's country name but still the combo box has no values in it. Do you get the picture. Please read the question again. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/212796-using-2-mysql_connect-functions-in-1-php-project/ Share on other sites More sharing options...
wildteen88 Posted September 8, 2010 Share Posted September 8, 2010 You can query more than one table per connection. You do not need to open another connection to query a different table. You may want to post your code here. Quote Link to comment https://forums.phpfreaks.com/topic/212796-using-2-mysql_connect-functions-in-1-php-project/#findComment-1108487 Share on other sites More sharing options...
theITvideos Posted September 8, 2010 Author Share Posted September 8, 2010 You can query more than one table per connection. You do not need to open another connection to query a different table. You may want to post your code here. The code for getting the visitors country is: There are 2 files. The 1st file has the class definition The 2nd file calls the class and displays visitors country to the page. File 1 - ip2country.php class ip2country { public $mysql_host='localhost'; public $db_name='mydb'; public $db_user='root'; public $db_pass=''; public $table_name='ip2c'; private $ip_num=0; private $ip=''; private $country_code=''; private $country_name=''; private $con=false; function ip2country() { $this->set_ip(); } public function get_ip_num() { return $this->ip_num; } public function set_ip($newip='') { if($newip=='') $newip=$this->get_client_ip(); $this->ip=$newip; $this->calculate_ip_num(); $this->country_code=''; $this->country_name=''; } public function calculate_ip_num() { if($this->ip=='') $this->ip=$this->get_client_ip(); $this->ip_num=sprintf("%u",ip2long($this->ip)); } public function get_country_code($ip_addr='') { if($ip_addr!='' && $ip_addr!=$this->ip) $this->set_ip($ip_addr); if($ip_addr=='') { if($this->ip!=$this->get_client_ip()) $this->set_ip(); } if($this->country_code!='') return $this->country_code; if(!$this->con) $this->mysql_con(); $sq="SELECT country_code,country_name FROM ".$this->table_name. " WHERE ". $this->ip_num." BETWEEN begin_ip_num AND end_ip_num"; $r=@mysql_query($sq,$this->con); if(!$r) return ''; $row=@mysql_fetch_assoc($r); $this->close(); $this->country_name=$row['country_name']; $this->country_code=$row['country_code']; return $row['country_code']; } public function get_country_name($ip_addr='') { $this->get_country_code($ip_addr); return $this->country_name; } public function get_client_ip() { $v=''; $v= (!empty($_SERVER['REMOTE_ADDR']))?$_SERVER['REMOTE_ADDR'] (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR']: @getenv('REMOTE_ADDR')); if(isset($_SERVER['HTTP_CLIENT_IP'])) $v=$_SERVER['HTTP_CLIENT_IP']; //print_r($_SERVER['REMOTE_ADDR']); //echo '<br/>'; return htmlspecialchars($v,ENT_QUOTES); } public function mysql_con() { $this->con=@mysql_connect($this->mysql_host,$this->db_user,$this->db_pass); if(!$this->con) return false; if( !mysql_query('USE ' . $this->db_name)) { $this->close(); return false; } return true; } public function get_mysql_con() { return $this->con; } public function create_mysql_table() { if(!$this->con) return false; mysql_query('DROP table ' . $this->table_name,$this->con); return mysql_query("CREATE table " . $this->table_name ." (id int(10) unsigned auto_increment, begin_ip varchar(20),end_ip varchar(20),begin_ip_num int(11) unsigned,end_ip_num int(11) unsigned,country_code varchar(3),country_name varchar(150), PRIMARY KEY(id),INDEX(begin_ip_num,end_ip_num))ENGINE=MyISAM",$this->con); } public function close() { @mysql_close($this->con); $this->con=false; } File 2 - which calls this class is DisplayCountry.php: include_once('ip2country\ip2country.php'); $ip2c=new ip2country(); echo 'Your country name is '. $ip2c->get_country_name() . '<br>'; echo 'Your country code is ' . $ip2c->get_country_code(); Now it works fine. But I also have another connection to other table which is lost while I am having this connection. The other code in the file that calls the connection is here: function makeConnection() { if (!($connection = mysql_connect(HOST, USER, PASSWORD))) { echo ("Could not connect"); return; } if (!(mysql_select_db(DATABASE, $connection))) displayerror(); return $connection; } The values for HOST and USER etc is defined in other file. This which works fine as long as we comment out the first previous connection in the ip2country.php file. First I thought that the previous connection does not get closed properly but it does so I can't see what the problem is here. What do you see here we can do to fix this. Quote Link to comment https://forums.phpfreaks.com/topic/212796-using-2-mysql_connect-functions-in-1-php-project/#findComment-1108540 Share on other sites More sharing options...
PFMaBiSmAd Posted September 8, 2010 Share Posted September 8, 2010 Wow, whoever wrote the ip2country code was not very kind to your database server. Each of the major functions, get_country_name() (which calls the other function internally) and get_country_code() opens and then closes the database connection surrounding the query. I would personally remove all the $this->close(); statements from that code. Alternatively, you could set the 4th parameter in the mysql_connect() statement in your makeConnection() function to TRUE so that it makes a new connection instead of reusing an existing connection. This assumes that your code is using the value that your function returns in each query as the connection link. Quote Link to comment https://forums.phpfreaks.com/topic/212796-using-2-mysql_connect-functions-in-1-php-project/#findComment-1108543 Share on other sites More sharing options...
theITvideos Posted September 8, 2010 Author Share Posted September 8, 2010 Wow, whoever wrote the ip2country code was not very kind to your database server. Each of the major functions, get_country_name() (which calls the other function internally) and get_country_code() opens and then closes the database connection surrounding the query. I would personally remove all the $this->close(); statements from that code. Alternatively, you could set the 4th parameter in the mysql_connect() statement in your makeConnection() function to TRUE so that it makes a new connection instead of reusing an existing connection. This assumes that your code is using the value that your function returns in each query as the connection link. Wow why do I get the feeling that phpfreaks is kind of a Crystal ball. Problem Solved bro!!!! Thank you!!! Quote Link to comment https://forums.phpfreaks.com/topic/212796-using-2-mysql_connect-functions-in-1-php-project/#findComment-1108546 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.