Jump to content

Could not connect: Unknown MySQL server host 'mysql_host' (11001)


newbienewbie

Recommended Posts

Newbie:

 

while executing below code i get

 

Could not connect: Unknown MySQL server host 'mysql_host' (11001).

 

Host is fine , i tried with ip also and with 127.0.0.1 also. but same error in all case. any suggestions.

 

<?php
$mysql_host='localhost';
$mysql_user='xyz';
$mysql_password='xyz';
$my_database='xyz';
$my_table='users';

// Connecting, selecting database
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
    or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('my_database') or die('Could not select database');

// Performing SQL query
$query = 'SELECT * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr>\n";
    foreach ($line as $col_value) {
        echo "\t\t<td>$col_value</td>\n";
    }
    echo "\t</tr>\n";
}
echo "</table>\n";

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>


Link to comment
Share on other sites

Try this:

<?php
$mysql_host='localhost';
$mysql_user='xyz';
$mysql_password='xyz';
$my_database='db';
$my_table='xyz';

// Connecting, selecting database
$link = mysql_connect($mysql_host, $mysql_user, $mysql_password)
    or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db($my_database) or die('Could not select database');

// Performing SQL query
$query = 'SELECT * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr>\n";
    foreach ($line as $col_value) {
        echo "\t\t<td>$col_value</td>\n";
    }
    echo "\t</tr>\n";
}
echo "</table>\n";

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>

Link to comment
Share on other sites

Glad that it works.

 

Always Remember some points :

1)Dont use like

$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')

bcoz 'mysql_host' is not valid, u r passing variable here, use $mysql_host and go.....

bcoz u define at the top that $mysql_host='localhost'.

This is variable $mysql_host='localhost'.

If u want to use "localhost" here :

$link = mysql_connect("localhost", "username", "password")

Then no need to define a variable at the top like thses:

$mysql_host='localhost';

 

2)

Always use this quotes "" for select or insert or update statement in query.

 

I hope u will git it, i am telling u bcoz u r newbie, if want to learn, then try to learn from

basics...

It will help u in future when u r going to make some CMS like sites.

Enjoy php,mysql

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.