[RESOLVED] problem accessing MySQL database
Hello,
my goal is to connect to a MySQL database using php.
I am using:
the code I'm using to try and connect to the database looks something like this:
Code:
<?php
$db = mysql_connect("localhost", "root", "mypassword");
echo "connection attempted";
?>
but the code doesn't even get to the echo command after the mysql_connect command!
so how am I supposed to set up the database so that I can access it? have I missed something out?
thanks:)
Re: problem accessing MySQL database
first of all, PHP5 doesn't have the MySQL extension enabled by default. if you don't know if it's enabled, find your php.ini file and search for this text: (extension=php_mysql.dll). if it's uncommented (comments are denoted by a semi-colon [";"]), then it would be enabled already. if it is commented, then remove the semi-colon, save the file and restart IIS. try running the script again.
if the MySQL extension is enabled and that is still not working, run this instead so that you can find out what the error is returning as:
PHP Code:
<?php
mysql_connect("host", "username", "password") or die(mysql_error());
?>
however, for the most part I must assume that you just don't have the MySQL extension enabled. PHP will only return a fatal error and stop parsing if a call to an undefined function is made (in this case, mysql_connect() is not a function because the MySQL extension creates that function) -- this would be the reason you couldn't see the echoed text. I don't believe a call to mysql_connect() that fails will cause a fatal error, but rather warning or notice.
Re: problem accessing MySQL database
hey, thanks for the instant reply :bigyello:
I found the php.ini file ( in the php program files directory? ) and uncommented the extensions line
but I still get the same fatal error!!!
Thanks for the help:)
Re: problem accessing MySQL database
What's the fatal error message?
Re: problem accessing MySQL database
I don't know! It doesn't display or show anything, and the page is terminated directly after the mysql_connect command, it doesn't even have the </body>
</html> tags in the source!
Re: problem accessing MySQL database
set up a PHP script that makes a call to phpinfo() and then search for "mysql." if you don't see it, then it still isn't enabled. if it still isn't enabled, did you not restart the IIS service?
also, try to find out where you're making IIS save log files to and look at them. suppressed error messages (due to php.ini settings) will be displayed there. you should see some fatal errors in there when loading whatever script you are.
Re: problem accessing MySQL database
yes, I tried restarting IIS, I have also restarted the computer seveal times.
The MySQL extension isn't enabled according to what you said and phpinfo()
Do you think it's possible that I could have installed MySQL or PHP in the wrong way, or the wrong order?! :confused:
Thanks all for the help :)
Re: problem accessing MySQL database
PHP and MySQL are completely independent, so it doesn't matter what order you install them in.
are you sure you uncommented the line with the MySQL extension and not the line with the MySQLi extension (should be right below it)? they are two different extensions and bring completely different sets of functions.
well, if you've think you have everything good on that front, make sure the extension_dir value in the php.ini file is pointing to the correct directory (should be "c:/your/path/to/php5/ext" -- mine is "e:/installs/php5.2.1/ext" [don't forget to enclose it in double-quotes, especially if it has any spaces in the path name]), and also make sure that the php_mysql.dll extension is inside of it. after changing it, save, and then restart IIS again.
Re: problem accessing MySQL database
Hey guess what?! I solved the problem!!!:D Thanks so much for bearing with me - you'll never realize just how much I appreciated your help.
The reason it wasn't working is because php hadn't installed the extensions by default, so I had to go back and change the installation.
First I tried enabling all of the extensions, but then php wouldn't work at all!!! So in the end I just enabled the mysql and a couple of others.
Have you any ideas why php wouldnt work at all with all the extensions installed?! ..which leads straight into another question- why aren't images loading correctly? Instead of seeing the image(picture), I only see the contents of the image file, ie the binary data:(
Thank you:)
Re: problem accessing MySQL database
if images will not load and only show binary data, then your browser isn't recognizing it; so you'll need to send the correct image headers (content-type) so it knows that it is, in fact, an image. this is a link to PHP.net's header() function. it has a bunch of information, too. generally, the content-type you'll need to send to the browser just depends on the file extension. for example:
PHP Code:
<?php
//if we were making a png image, we use:
header("Content-type: image/png");
//jpg image:
header("Content-type: image/jpeg");
//gif:
header("Content-type: image/gif");
?>
oh, and PHP might not work with every extension installed because of some conflict errors, I guess. some extensions might not work with others? I've never tried enabling them all.
PS: using the PHP installer is a bad idea if you ask me. it's much better go with the zipped version. I'm not sure what the installer is actually supposed to do, but it has always caused problems for people I know that have used it.. and I just find it easy to install things manually.
Re: problem accessing MySQL database
php.ini can be a pain at first, so can mysql, another option is to use apache as xampp has all you need enabled by default, all you need to do is start the apache and mysql services.....
Though you might want to check some security if you went this route....