newbienewbie Posted March 1, 2007 Share Posted March 1, 2007 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); ?> Quote Link to comment Share on other sites More sharing options...
Greaser9780 Posted March 1, 2007 Share Posted March 1, 2007 Just for fun, where you have localhost try mysql. For my connection that's what I needed. Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted March 1, 2007 Share Posted March 1, 2007 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); ?> Quote Link to comment Share on other sites More sharing options...
newbienewbie Posted March 1, 2007 Author Share Posted March 1, 2007 thanks mmarif4u it worked with few modification, don't know why single quotes doesn't works $query = "SELECT * FROM $my_table"; Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted March 1, 2007 Share Posted March 1, 2007 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 Quote Link to comment Share on other sites More sharing options...
newbienewbie Posted March 1, 2007 Author Share Posted March 1, 2007 thanks , i will keep in mind ur tips. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.