{"id":269,"date":"2014-03-28T10:45:13","date_gmt":"2014-03-28T08:45:13","guid":{"rendered":"http:\/\/mednis.info\/wp\/?p=269"},"modified":"2014-03-28T11:01:12","modified_gmt":"2014-03-28T09:01:12","slug":"mysqli-unknow-server-host","status":"publish","type":"post","link":"http:\/\/mednis.info\/wp\/?p=269","title":{"rendered":"Mysqli: Unknow server host"},"content":{"rendered":"<p>PHP can fail to connect to a MySQL database when a custom port number is specified.<br \/>\nIn the old-style you would write like this:<br \/>\n<code>$link = mysql_connect(\"dbhost.info:6603\", \"dbuser\", \"dbpaswd\",);<\/code><\/p>\n<p>Now with mysqli the same approach does not work.<br \/>\n<code>$mysqli = new mysqli(\"dbhost.info:6603\", \"dbuser\", \"dbpaswd\", \"databasename\");<\/code><br \/>\nIf you try to connect this way using mysqli, it will fail with an error <em>Unknow server host &#8216;dbhost.info:6603&#8217;<\/em>:<\/p>\n<p><!--more--><\/p>\n<p>The problem is that MySQLi class does not accept the port as part of the hostname, but as a separate parameter.<\/p>\n<p>Here is the correct way:<br \/>\n<code>$mysqli = new mysqli(\"dbhost.info\", \"dbuser\", \"dbpaswd\", \"databasename\", \"6603\");<\/code><\/p>\n<p><strong>The complete code sample<\/strong><br \/>\n<code>&lt;?php<br \/>\n$mysqli = new mysqli(\"dbhost.info\", \"dbuser\", \"dbpaswd\", \"databasename\", \"3306\");<br \/>\nif ($mysqli->connect_errno)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp; print($mysqli->connect_errno);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp; print($mysqli->connect_error);<br \/>\n}<br \/>\nelse{print(\"Connected!\");}<br \/>\n?&gt;<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP can fail to connect to a MySQL database when a custom port number is specified. In the old-style you would write like this: $link = mysql_connect(&#8220;dbhost.info:6603&#8221;, &#8220;dbuser&#8221;, &#8220;dbpaswd&#8221;,); Now with mysqli the same approach does not work. $mysqli = new mysqli(&#8220;dbhost.info:6603&#8221;, &#8220;dbuser&#8221;, &#8220;dbpaswd&#8221;, &#8220;databasename&#8221;); If you try to connect this way using mysqli, it &hellip; <a href=\"http:\/\/mednis.info\/wp\/?p=269\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Mysqli: Unknow server host<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,28],"tags":[],"_links":{"self":[{"href":"http:\/\/mednis.info\/wp\/index.php?rest_route=\/wp\/v2\/posts\/269"}],"collection":[{"href":"http:\/\/mednis.info\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mednis.info\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mednis.info\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/mednis.info\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=269"}],"version-history":[{"count":7,"href":"http:\/\/mednis.info\/wp\/index.php?rest_route=\/wp\/v2\/posts\/269\/revisions"}],"predecessor-version":[{"id":276,"href":"http:\/\/mednis.info\/wp\/index.php?rest_route=\/wp\/v2\/posts\/269\/revisions\/276"}],"wp:attachment":[{"href":"http:\/\/mednis.info\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mednis.info\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=269"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mednis.info\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}