I have the following , relatively simple connection script to a database :
The below script is just creating a generic interface :
<?php
//Filename: IConnectInfo.php
interface IConnectInfo
{
const HOST ="localhost";
const UNAME ="root";
const PW ="";
const DBNAME = "login";
public static function doConnect();
}
?>
The script below, that makes use of the above interface:
<?php
//FILENAME :: UniversalConnect.php
ini_set("display_errors","1");
ERROR_REPORTING( E_ALL | E_STRICT );
include_once('IConnectInfo.php');
class UniversalConnect implements IConnectInfo
{
private static $server=IConnectInfo::HOST;
private static $currentDB= IConnectInfo::DBNAME;
private static $user= IConnectInfo::UNAME;
private static $pass= IConnectInfo::PW;
private static $hookup;
public static function doConnect()
{
self::$hookup=mysqli_connect(self::$server, self::$user, self::$pass, self::$currentDB);
if(self::$hookup)
{
echo "Successful connection to MySQL:<br/>";
}
elseif (mysqli_connect_error(self::$hookup))
{
echo('Here is why it failed: ' . mysqli_connect_error());
}
return self::$hookup;
}
}
?>
Now till here everything is neat and clean and works fine :
I tested the above two files are working , by adding the following 2 lines at the end of the above file .
$instance = new UniversalConnect();
$instance::doConnect();
I get a message "Successful connection to MySQL:" , perfect !!!
now comes the 3rd file : (I have commented out some of the code to make things simple) :
<?php
//FILENAME DataEntry.php
require_once('tablework/UniversalConnect.php');
class DataEntry
{
//Variable for MySql connection
private $hookup;
private $sql;
private $tableMaster;
//Field Variables
private $name;
private $email;
private $lang;
public function __construct()
{
//Get table name and make connection
$this->tableMaster="basics";
if($this->hookup=UniversalConnect::doConnect()){
echo "<b>connected</b>";
}else{
echo "<b>Not connected</b>";
}
}
}
$instance = new DataEntry();
?>
Now when i run the above file , somehow the connection to the database fails ! even though in UniversalConnect.php
the connection is successful !
The error i get is
Here is why it failed: php_network_getaddresses: getaddrinfo failed: No such host is known. Not connected.
I really don't understand why when the connection is made in UniversalConnect.php
and the same connection is being returned to dataEntry.php
, does the connection Fail ! .
EDIT :: List of errors :
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\Login2.0\tablework\UniversalConnect.php on line 19
Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\Login2.0\tablework\UniversalConnect.php on line 19 Here is why it failed: php_network_getaddresses: getaddrinfo failed: No such host is known. Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\Login2.0\tablework\UniversalConnect.php on line 19
Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\Login2.0\tablework\UniversalConnect.php on line 19 Here is why it failed: php_network_getaddresses: getaddrinfo failed: No such host is known. Not connected.
I would appreciate any help .
Thank you .
Tenali .