Jump to content

[SOLVED] Connecting to MySQL with PHP


Raol

Recommended Posts

I want to connect to MySQL using php. The code i've seen used in a lot of examples is along the lines of:

 

<?php

$username = "username";

$password = "password";

$hostname = "localhost";

                $connection = mysql_connect($db_host, $db_username, $db_password);

                if (!$connection){

                      die ("Could not connect to the database: <br />". mysql_error( ));

                }

?>

 

Now, despite whether I assign a correct username/password pair, or an incorrect pair, all it does is send me to a blank page. I've tried making a query after putting in a correct username/password but nothing displayed on the page still. I don't get any errors, just a blank page.

 

Does anyone know what I could be doing wrong? I imagine i'd get an error if it couldn't find the mysql_connect function or something like that.

 

Thanks in advance,

 

-Raol

Link to comment
Share on other sites

Here is what I use:

 

<?php # mysql.php

// Define the server and database variables.

define ('DB_USER', '');		// MySQL username
define ('DB_PASS', '');	             // MySQL password
define ('DB_HOST', 'localhost');	// MySQL hostname
define ('DB_NAME', '');		// MySQL database

// Establish the MySQL connection and then select the database.

$dbc = mysql_connect (DB_HOST, DB_USER, DB_PASS) OR die ('Cannot connect to MySQL server.');
mysql_select_db (DB_NAME) OR die ('Cannot connect to database.');

?>


Link to comment
Share on other sites

haha yea I have no problems with regular php commands. MySQL shows up in phpinfo(). I'm just going through this book that deals with php and sql, and I got through the sections teaching all the commands of php such as classes and loops and arrays, and was able to display all the examples in the book and my own problems fine. And the section dealing with MySQL worked perfectly too. But now I'm at the section combining php and mysql, but I can't seem to get passed this connection issue. I'm using the extension, or I'd assume it would give me an error about not being able to find the function mysql_connect, but it doesn't do anythin, it just sends me to a blank white page.

Link to comment
Share on other sites

Raol, instead of $hostname = "localhost";

put $hostname = "mysql";

 

Unlikely. I've only ever seen two alternates for hostname. localhost in almost every case, and very rarely what's effectively a URL. Either way, your host will have relevant information online somewhere. My money's still on error display being off.

Link to comment
Share on other sites

Hi,

For what it's worth, my providor (ipower.com) recently 'updated' their servers.... my  MySQL stopped working

After spitting and sputtering... I had to change the old standby term of 'localhost' to

mypersonalurl.ipowermysql.com

 

Their 2nd tier tech's said 'localhost' no longer works... and ya know.. it doesnt,  but after running down all my 'localhost' references and replaced them with...mypersonalurl.ipowermysql.com

voila.. life is good now...

 

Check with your providor

 

Lots of luck.

 

Link to comment
Share on other sites

perhaps error display is OFF even though error reporting is on

 

http://ca.php.net/manual/en/errorfunc.configuration.php#ini.display-errors

 

thank you very much! I woulda thought error reporting woulda been on by default but i guess not. So now it showed me the error informing me that it could not find the mysql_connect() function, so i just reinstalled PHP and used the windows installer to install the extensions and it works!

 

Thanks to everyone who responded, cheers!

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.