1

This might be a stupid question but, I am trying to connect to a database trough a PHP file:

<?php
$con = mysql_connect("host","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM wp_posts");

while($row = mysql_fetch_array($result))
{
echo $row['post_title'] . " " . $row['ID'];
echo "<br />";
}
mysql_close($con);
?>

But I am getting the following error: Could not connect: Unknown MySQL server host 'ijsselmondenieuws.nl/' (11004)

I am confused about what host I need to use. The website itself is www.ijsselmondenieuws.nl. For my own website I know its mysql50-c1.website.com.

Any help is appreciated!

Thanks,

Jef

2
  • You'll have to find out from your hosting company's tech support what to use. Commented Feb 4, 2012 at 23:37
  • Where does this database reside? "localhost" doesn't work? Did you try the database server IP? Commented Feb 4, 2012 at 23:37

4 Answers 4

2

I am able to connect to ijsselmondenieuws.nl on port 3306 and get a MySQL greeting. Make sure there isn't a / on the end of the host as it appears to show in the error message.

In order to authenticate from a different host, you may need to explicitly allow your user to connect from your IP/hostname if it is not on the server.

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

Comments

1

The host will either by localhost if the MySQL server is running on the same machine as the webserver, or a local IP address like 192.168.xxx.xxx or 10.xxx.xxx.xxx. Very rarely will the host be a name, in which case it's most likely a computer name (for example mine is KOLINK-PC).

5 Comments

That's not necessarily true; shared servers, for instance, oftentimes will have non-localhost URIs, as well as where I work (at a university). It just depends on how the server environment has been setup and how they've provided the services to your scripts.
My servers connect to a local IP, such as I mentioned in my answer.
Yes, I see what you mean (didn't really get that last part at first). We have a load balancer at work, so we actually have to use something along the lines of mysql.webadmin.example.edu, which could return one of four servers. Dreamhost also has you use a mysql.dbname.dreamhosters.net syntax. I'm just saying there's more variation than what you're alluding to, and that the host company's documentation should be consulted (if it's a hosted site short of full-blown virtual server you're administering yourself).
I was on Dreamhost before, I thought they had it on localhost... Guess they changed since I left.
It's changed I suppose. I'm not real sure why they do it this way (which I think is a little unusual), but it must have something to do with how they manage the shared environment.
1

You have to look in your information you got from your host. If everything is right, it lists a database server and port you should use. If you can't find this, contact your host.

(I am assuming you aren't hosting the website yourself. In that case, obviously, the correct approach would be different)

Comments

1

The mysql server in ijsselmondenieuws.nl must have a user who can access from you local pc.

Create a user there like following.

GRANT 
    ALL PRIVILEGES 
ON 
    my_db.*  
TO 
    user1@LOCALHOST_EXTERNAL_IP 

IDENTIFIED BY 

    'PASSWORD';

Now connect form your localhost by following line.

    $con = mysql_connect("ijsselmondenieuws.nl","user1","PASSWORD");

1 Comment

Doesn't MySQL give a different error message if it can connect, but can't log in?

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.