0

I have a really strange problem. I want to load the MySQL connection attributes from an XML file, but I got this error when I try to process the connection:

php_network_getaddresses: getaddrinfo failed (Error number: 2002)

And why this is strange? Because if I just write down the mysql_connect() statement myself, and not from the xml file, it works.

The XML file looks like that:

<?xml version="1.0" encoding="UTF-8"?>
 <data>
   <host>"localhost"</host>       
   <username>"root"</username>
   <password>"password"</password>
   <database>"account"</database>
 </data>    

The PHP part:

function connection() {
   $file = 'access.xml';
   if(file_exists($file)) {
      $xml = simplexml_load_file($file);
      $host = $xml->host;
      $name = $xml->username;
      $password = $xml->password;
      $database = $xml->database;

      $connect = mysql_connect($host,$name,$password);
      //mysql_select_db($database) or die("Adatbázis csatlakozás sikertelen.");
      } else {
          print "XML betöltése sikertelen. - MySQL csatlakozás sikertelen."; // Error message in hungarian language
      }
}

I thought about maybe the charset causes the problem, but no, because I already set the PHP's charset to UTF-8, my SQL server's charset is UTF-8, too, and as you can see, the XML file uses UTF-8. So, I really don't know what's the problem.

Thanks for the help,

Tomco

2 Answers 2

2

I have a sneaking suspicion it's the quotes in your XML. Try:

<?xml version="1.0" encoding="UTF-8"?>
 <data>
   <host>localhost</host>       
   <username>root</username>
   <password>password</password>
   <database>account</database>
 </data> 

Quotes are used in your code to identify strings... But the XML parser already knows that it's working with strings, so it will treat the quotes you gave it as part of that text. So instead of expanding to:

$connect = mysql_connect('localhost','account','password');

You're actually expanding to:

$connect = mysql_connect('"localhost"', '"account"', '"password"');

And while PHP can't find a server called "localhost", it shouldn't have problems finding one called localhost.

Sign up to request clarification or add additional context in comments.

Comments

1

remove the quotes in your access.xml file to:

<?xml version="1.0" encoding="UTF-8"?>
 <data>
   <host>localhost</host>       
   <username>root</username>
   <password>password</password>
   <database>account</database>
 </data>

it will work now

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.