0

I have this simple code:

<?php
//Open the mySQL connection
$conn_string = "'localhost', 'Vale', 'test'";
$dbh = mysql_connect($conn_string);

//Check that a connection with the DB has been established
if (!$dbh) 
{
   die("Error in MySQL connection: " . mysql_error());
}
...

And I get the error: Error in MySQL connection: php_network_getaddresses: getaddrinfo failed: The requested name is valid, but no data of the requested type was found.

I cannot figure out what the problem is, I have been google-ing but all the suggestions have failed (tried 127.0.0.1 instead of localhost, 127.0.0.1:3306, etc.)

I have code that works with postgre, but I need to use mysql, and I am trying to modify it, but I cannot pass the first line and get a connection. Any suggestion, please? Thank you!

4 Answers 4

4

mysql_connect doesn't take a comma seperated string. It takes 3 individual strings. Change it to this:

$dbh = mysql_connect($server, $mysql_user, $password);

If you absolutely have a comma separated string, and can't get around this, you can split the string like this:

 $config = str_replace("'", '', $conn_string); // replace the quotes.
 $config = preg_split('/,/', $conn_string); // split string on ,
 if($config != $conn_string) { // make sure this returned an array
     count($config) === 3 OR die("Invalid database configuration string");
     $dbh = mysql_connect($config[0], $config[1], $config[2]);
     if(FALSE === $dbh) {
         die("Coult not connect: " . mysql_error());
     }
 } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I just realized that! New to MySQL, in Postgre it worked.
1

You might want to consider MySQLi, instead of MySQL.

<?php

$dbh=mysqli_connect("localhost","Vale","Password","test"); /* Vale is your username? And the name of your database if test, right? And your Username's Password is blank? */

if(mysqli_connect_errno()){

echo "Error".mysqli_connect_error();
}

?>

1 Comment

You should probably make it clear for the OP that the 3rd argument that's a blank string is actually the DB password.
0

As other users have pointed out mysql_connect expects the database, username, and password as separate arguments rather than a single string.

I think another highly important issue to point out is that this particular extension is deprecated.

Please see: https://www.php.net/function.mysql-connect

A better solution would be to use mysqli_connect: https://www.php.net/manual/en/function.mysqli-connect.php

$db = mysqli_connect( 'localhost', 'Vale', 'test', 'yourDatabaseName' );

Comments

0

mysql_connect requires three argument not single string

$dbh = mysql_connect('localhost', 'Vale', 'test');

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.