/    Sign up×
Community /Pin to ProfileBookmark

Oops connection fails when called on 2nd instance

guys i have the following , relatively simple connection script to a database : [The code is clean , so don’t worry . ]

The below script is just creating a generic interface :

[CODE]
<?php
//Filename: IConnectInfo.php
interface IConnectInfo
{
const HOST =”localhost”;
const UNAME =”root”;
const PW =””;
const DBNAME = “login”;

public static function doConnect();
}

?>
[/CODE]

The script below, that makes use of the above interface:

[CODE]
<?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;
}

}

?>
[/CODE]

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 .

[CODE]
$instance = new UniversalConnect();
$instance::doConnect();
[/CODE]

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) :

[CODE]
<?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();

?>
[/CODE]

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 ! .

I would appreciate any help .

Thank you .

Tenali .

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 22.2014 — I'd start by outputting those connection parameters as part of the error output to see what values (if any) they have.
Copy linkTweet thisAlerts:
@gautamz07authorDec 22.2014 — Nice ! lemme try that and comeback !
×

Success!

Help @gautamz07 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...