Page 1 of 1

Unknown server host 'database'.

Posted: Mon Jun 08, 2009 7:04 am
by zenix
Hi, I am new to php and have been doing quite well learning it. I have just come upon a problem that has me completely stumped however. I can successful create the database, select the table and even add content. When I attempt viewing the content I get a message that says "Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'database3' (11004)" I get this even though at the other points before it (create database, create and select table) all come back with Successfully connected and selected. The add content page has just 2 fields, subject and body. I get no errors on tis page, it appears to be working well. If anyone has any idea for me to try I'd really appreciate if they'd let me know. Thanks a lot in advance! Here is the code for viewing the input data. It's supposed to be very simple.

Code: Select all

 
<html>
<head>
<title>View Database</title>
</head>
 
<body>
 
<?php
 
ini_set('display_errors', 1);
error_reporting(E_ALL);
 
//connect and select
if($dbc = mysql_connect('localhost','root'))
{
    if(!mysql_connect('database3'))
    {
        
        
        die('<p>Could not select the database because <b>' . mysql_error() . '</p></b>');
 
    }
}
else
{
    die('<p>Could not connect to mysql because:<b>' . mysql_error() . '</p></b>');
}
//define query
$query = 'SELECT * FROM db_entries ORDER BY date_entered DESC';
 
if($r = mysql_query($query))
{
    //retrieve and print records
    while($row = mysql_fetch_array($r))
    {
        print "<p><h3>{$row['title']}</h3>
        {$row['entry']}<br/>
        <a href=\"edit_entry.php?id={$row['db_id']}\">Edit</a>
        <a href=\"delete_entry.php?id={$row['db_id']}\">Delete</a></p><hr/>\n";
    }
}
else
{
    //Query didn't run
    die('<p>Could not retrieve data because <b>' . mysql_error() . "the query was $query</b></p>");
}
mysql_close();
?>
 
                                                                  
            
                        
 
 
</body>
</html>

Re: Unknown server host 'database'.

Posted: Mon Jun 08, 2009 7:23 am
by jayshields
Change your second call to mysql_connect() to a call to mysql_select_db().

Re: Unknown server host 'database'.

Posted: Mon Jun 08, 2009 7:48 am
by zenix
Thanks Jay, that was a foolish thing for me to over look. Now when I add content to the db and try viewing it all I get is a blank page. Any thoughts?

Re: Unknown server host 'database'.

Posted: Mon Jun 08, 2009 10:41 am
by jayshields
Make sure there are some rows in your table. You could try using mysql_query() or die(mysql_error()) instead of your if statement set up you have now.