Raol Posted April 19, 2008 Share Posted April 19, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/101902-solved-connecting-to-mysql-with-php/ Share on other sites More sharing options...
Eiolon Posted April 19, 2008 Share Posted April 19, 2008 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.'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/101902-solved-connecting-to-mysql-with-php/#findComment-521505 Share on other sites More sharing options...
BlueSkyIS Posted April 19, 2008 Share Posted April 19, 2008 if you're not seeing anything regardless of connection params, you may need to turn on error_reporting Quote Link to comment https://forums.phpfreaks.com/topic/101902-solved-connecting-to-mysql-with-php/#findComment-521506 Share on other sites More sharing options...
Raol Posted April 19, 2008 Author Share Posted April 19, 2008 Eiolon: still just gives me a blank page, even if i input random user names like "fjsdtnlksna". =( BlueSkyIS: How do i turn on error_reporting? Quote Link to comment https://forums.phpfreaks.com/topic/101902-solved-connecting-to-mysql-with-php/#findComment-521509 Share on other sites More sharing options...
BlueSkyIS Posted April 19, 2008 Share Posted April 19, 2008 try error_reporting(E_ALL); at the top of your script Quote Link to comment https://forums.phpfreaks.com/topic/101902-solved-connecting-to-mysql-with-php/#findComment-521513 Share on other sites More sharing options...
Raol Posted April 19, 2008 Author Share Posted April 19, 2008 Still a blank page =S. Quote Link to comment https://forums.phpfreaks.com/topic/101902-solved-connecting-to-mysql-with-php/#findComment-521518 Share on other sites More sharing options...
BlueSkyIS Posted April 19, 2008 Share Posted April 19, 2008 can you do this? ??? <?php echo "hello world"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/101902-solved-connecting-to-mysql-with-php/#findComment-521521 Share on other sites More sharing options...
Raol Posted April 19, 2008 Author Share Posted April 19, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/101902-solved-connecting-to-mysql-with-php/#findComment-521541 Share on other sites More sharing options...
AndyB Posted April 19, 2008 Share Posted April 19, 2008 perhaps error display is OFF even though error reporting is on http://ca.php.net/manual/en/errorfunc.configuration.php#ini.display-errors Quote Link to comment https://forums.phpfreaks.com/topic/101902-solved-connecting-to-mysql-with-php/#findComment-521599 Share on other sites More sharing options...
dezkit Posted April 19, 2008 Share Posted April 19, 2008 Raol, instead of $hostname = "localhost"; put $hostname = "mysql"; Quote Link to comment https://forums.phpfreaks.com/topic/101902-solved-connecting-to-mysql-with-php/#findComment-521606 Share on other sites More sharing options...
AndyB Posted April 19, 2008 Share Posted April 19, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/101902-solved-connecting-to-mysql-with-php/#findComment-521616 Share on other sites More sharing options...
dezkit Posted April 20, 2008 Share Posted April 20, 2008 im just thinking that maybe hes using an online sql or something, same thing happened to me. Quote Link to comment https://forums.phpfreaks.com/topic/101902-solved-connecting-to-mysql-with-php/#findComment-521634 Share on other sites More sharing options...
MesaFloyd Posted April 20, 2008 Share Posted April 20, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/101902-solved-connecting-to-mysql-with-php/#findComment-521640 Share on other sites More sharing options...
Raol Posted April 20, 2008 Author Share Posted April 20, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/101902-solved-connecting-to-mysql-with-php/#findComment-521696 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.