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); ?> Link to comment https://forums.phpfreaks.com/topic/40658-could-not-connect-unknown-mysql-server-host-mysql_host-11001/ 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. Link to comment https://forums.phpfreaks.com/topic/40658-could-not-connect-unknown-mysql-server-host-mysql_host-11001/#findComment-196706 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); ?> Link to comment https://forums.phpfreaks.com/topic/40658-could-not-connect-unknown-mysql-server-host-mysql_host-11001/#findComment-196708 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"; Link to comment https://forums.phpfreaks.com/topic/40658-could-not-connect-unknown-mysql-server-host-mysql_host-11001/#findComment-196716 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 Link to comment https://forums.phpfreaks.com/topic/40658-could-not-connect-unknown-mysql-server-host-mysql_host-11001/#findComment-196724 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. Link to comment https://forums.phpfreaks.com/topic/40658-could-not-connect-unknown-mysql-server-host-mysql_host-11001/#findComment-196733 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.