SSWWW
SSWWW

Reputation: 33

What in this program

I have been complete my class that will connecting to mysql server. When I running it I got an error:

Warning: mysql_connect() [function.mysql-connect]: php_network_getaddresses: getaddrinfo failed: This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative server. in C:\wamp\www.php on line 18

Warning: mysql_connect() [function.mysql-connect]: [2002] php_network_getaddresses: getaddrinfo failed: This is usually a (trying to connect via tcp://test:3306) in C:\wamp\www.php on line 18

Warning: mysql_connect() [function.mysql-connect]: php_network_getaddresses: getaddrinfo failed: This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative server. in C:\wamp\www.php on line 18 Error cannnot connect to mysql server

I check my sql server that it had high privileges and dont have a password.

my code:

<?php
class mysql
{
    var $user;
    var $password;
    var $database;
    var $host;

    function mysql($username, $password, $database, $host)
    {
        $this->user = $username;
        $this->password = $password;
        $this->database = $database;
        $this->host = $host;
    }
    function connect()
    {
        $conn = mysql_connect($this->host, $this->user, $this->password, $this->database)
                or die("Error cannnot connect to mysql server");
        echo "Connected successfully to Mysql server";
        mysql_close($conn);
    }
}
$connect = new mysql('127.0.0.1','root','','test');
$connect->connect();
?>

Upvotes: 1

Views: 1300

Answers (2)

bitfox
bitfox

Reputation: 2292

I suggest you to defined each property:

var $user;
var $password;
var $database;
var $host;

like these:

private $user;
private $password;
private $database;
private $host;

And create setter and getter methods for each property, an example:

public setUser($user){ 
   $this->user = $user; 
}

public getUser(){
   return $this->user;
}

Now, I'm sure you are asking yourself: Why? Here there is the answer :-)

bye

Upvotes: 0

Arthur Halma
Arthur Halma

Reputation: 4001

try:

$connect = new mysql('root','','test','127.0.0.1');

Upvotes: 6

Related Questions