Jump to content

[SOLVED] PHP will not connect to mysql


barry6628

Recommended Posts

I have a Widows XP Pro system tring to use PHP and MySQL 5 with Apache Server. The Apache seems ok as I use it with Dreamweaver, PHP works OK, MySQL 5 Works from the command line.  When I run a simple connection script I get left wih a blank browser as if nothing is happening. I have run phpinfo.php

and mysql is listed.  I have checked that both Apache and mysql sevises are running and as you can gess ive tried alsort still no luck.  Can you help.

 

Regards

 

Barry

Link to comment
Share on other sites

What gets displayed when you turn display_errors on within the php.ini (make sure you restart Apache for new changes to take affect). Re run your script again. If there is a problem with script PHP should display an error to the screen rather than a blank screen.

 

Also can you post your code here too.

Link to comment
Share on other sites

Hi and thaks for you contact. Im convince the problem is the way im tring to use php to connect to mysql last night i tried using command line prompt and eventulay came up with this.

 

c:\Programe Files\mysql\mysql server5\bin> Then I typed  mysql -u barry@localhost root mysql

 

This opend all the variables. How do i change this so i can write php to connect with mysql.

 

I would post you the code i used but ive changed it so much its now a mess. I copied straight out of the book Beginning PHP and MySQL 5 and I tried loads of codes of the net. Time to start again I think.

 

Hope this dosent put you off helping me.

 

Regards

 

Barry

Link to comment
Share on other sites

In your php script you can use the various mysql_* functions for working with MySQL databases in PHP. You don't need to use command line in php scripts. For example you use mysql_connect to connect to MYSQL server. mysql_query to run an sql query.

 

I would recommend you to go through the tutorials over at php-mysql-tutorial.com/ for learning how to use PHP with MySQL.

 

NOTE: If you are using PHP5 then you'll need to configure PHP to use the MySQL extension. You can do this by reading this FAQ.

Link to comment
Share on other sites

Hi sorry for confusing the issue. i was only using the command prompt to prove mysql was working this is the bit of code I use and it reports cannot connect to server

 

<?php

 

 

mysql_connect("localhost","barrymd@localhost","24086628") or die("Could not connect to server!");

echo "connected<br />";

@mysql_select_db("books") or die( "Unable to select database");

echo "selected db";

 

$query = "INSERT INTO contacts VALUES ('','$first','$last','$phone','$mobile','$fax','$email','$web')";

mysql_query($query);

 

mysql_close();

 

echo "all done";

?>

 

regards and many thanks for your time so far.

 

Link to comment
Share on other sites

Rather than displaying 'Could not connect to server!' when the connection fails place the mysql_error() function within the die statement too. That way you get an error from mysql which which will tell what is wrong

 

New code:

<?php

mysql_connect("localhost","barrymd@localhost","24086628") or die("Could not connect to server:<br />" . mysql_error());

echo "connected";

mysql_select_db("books") or die( "Unable to select database:<br />" . mysql_error());

echo "selected db";

$query = "INSERT INTO contacts VALUES ('','$first','$last','$phone','$mobile','$fax','$email','$web')";
mysql_query($query) or die( "Unable to run query (<tt>$query</tt>:<br />" . mysql_error());

mysql_close();

echo "all done";
?>

Link to comment
Share on other sites

Your php.ini is fine. just tried on my local install of AMP and Apache started up with no errors, however I did modify the extension_dir line from C:/php5/ext (which your path) to C:/Server/php/ext (which is my path).

 

I'm not sure what to suggest.

 

Looks like everything is setup correctly. however for some reason PHP is ignoring the username/password credentials you have set up within the mysql connect function and tying to connect to mysql with the username 'SYSTEM'.

 

EDIT. Oh hang on. It is to do with your php.ini. when I use mysq_connect with my username/password credentials I get the same error as you are getting. However If I swap your php.ini with my php.ini I connect to mysql fine.

 

EDIT2: Ohh! I think I just solved it. You had a setting called sql.safe_mode on within the php.ini. When you enable this setting PHP will ignore any username/password credentials you use with in mysql_connect function and this is why you are getting the error message. Turn that setting off and you shouldn't get that error message again.

Link to comment
Share on other sites

Are you sure php is reading the php.ini you are editing. You can check this by running phpinfo() function within a php script then looking for the line that starts with Loaded Configuration File. This line should show the full path to the php.ini PHP is loading for it's configuration. Is the path stated the same path to the php.ini you are editing?

 

NOTE: If you don't get a Loaded Configuration File line then look for a line that starts with Configuration File (php.ini) Path instead.

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.