Jump to content

Php Script Fails To Connect To Mysql


chibineku

Recommended Posts

I have been trying to configure MySQL, PHP and Apache, and I thought I had it figured out, but on running the following trial script, I get no output at all:

<?php$mysqli = new mysqli("localhost","********","********","test");if (mysqli_connect_errno()) {printf("Connect failed: %s\n", mysqli_connect_error());exit();} else {printf("Host information: %s\n", mysqli_get_host_info($mysqli));}?>

Obviously I am replacing the asterisks with a valid username and password. It ought to at least display an error, but I get nothing. Other PHP scripts work fine and my MySQL instance runs from the command line without any problems.And ideas? I feel like such a noob these days with all the basic installation threads I'm starting.

Link to comment
Share on other sites

This is how I do it and it works fine and is easy to fix if any problems arise.

<?php$con = mysql_connect("server", "name", "password");if(!$con){	 echo 'Could not connect to the database: ', die(mysql_error());}?>

But maybe you're trying to do more then just connecting the database? Because I've never seen mysqli_get_host_info.Are you sure mysql should have an 'i' at the end of it?Edit: Oh, and when I'm debugging, I like to set up alerts everywhere in my code to see exactly where it stops working-

<?php$con = mysql_connect("server", "name", "password");echo '<script type="text/javascript">'echo 'alert("1");'; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////echo '</script>'if(!$con){echo '<script type="text/javascript">'echo 'alert("2");'; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////echo '</script>'	 echo 'Could not connect to the database: ', die(mysql_error());echo '<script type="text/javascript">'echo 'alert("3");'; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////echo '</script>'}echo '<script type="text/javascript">'echo 'alert("4");'; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////echo '</script>'?>

See? Now when you see alerts 1-2 then you know there's a problem with your error message and it's congesting the whole thing.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...