Sephern Posted February 25, 2009 Share Posted February 25, 2009 Hey. I'm just learning PHP, and it seems like a pretty easy problem to solve, but I can't seem to get the problem. I've got the basics of PHP, so I'm moving on to coding which uses a database. I've got a testing server set up with Apache, PHP and mysql installed, made a database and everything should all work fine. Now, I want to connect to my database, so that I can carry out database tasks. <?php $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_close($link); ?> This is the code I'm using. In theory it should feedback if it connects to the database, and feed back if it doesn't. However, when I run the actual script, there's no text feedback whatsoever. Any ideas why this is occurring? Quote Link to comment https://forums.phpfreaks.com/topic/146939-new-to-php/ Share on other sites More sharing options...
premiso Posted February 25, 2009 Share Posted February 25, 2009 Turn on display_errors ini_set('display_errors', 1); to give you error messages. as to why it is not working, your code looks sound to me, no syntax issues. Also mysql_close is not needed, the connection is closed when the page has finished executing. Try this as well: <?php echo 'Attempting a connection'; $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; ?> And see what you get back. Quote Link to comment https://forums.phpfreaks.com/topic/146939-new-to-php/#findComment-771424 Share on other sites More sharing options...
Sephern Posted February 25, 2009 Author Share Posted February 25, 2009 I closed it because I was troubleshooting the greater code, and wanted to find if it was a problem connecting to the db or later in the script. Results: Nothing if I put echo 'Attempting a connection' beforehand. Fatal error: Call to undefined function mysql_connect() in C:\Program Files\Apache Group\Apache2\htdocs\test.php on line 3 If I don't. Quote Link to comment https://forums.phpfreaks.com/topic/146939-new-to-php/#findComment-771427 Share on other sites More sharing options...
premiso Posted February 25, 2009 Share Posted February 25, 2009 It sounds like you do not have the mysql extension installed via php. You have to either uncomment that line in the php.ini and/or install that extension. Should be as simple as uncommenting, saving then restarting apache. Quote Link to comment https://forums.phpfreaks.com/topic/146939-new-to-php/#findComment-771429 Share on other sites More sharing options...
Sephern Posted February 25, 2009 Author Share Posted February 25, 2009 Everything regarding Mysql in my PHP.ini file follows this format- ; Allow or prevent persistent links. mysql.allow_persistent = On I Presumed that the first part (Allow or prevent persistent links) was a comment telling the installer what the option does, and the second part was the important bit. If this is the case, then it should work... or was I wrong in this assumption? Quote Link to comment https://forums.phpfreaks.com/topic/146939-new-to-php/#findComment-771433 Share on other sites More sharing options...
fry2010 Posted February 25, 2009 Share Posted February 25, 2009 Try this. Go to Control Panel on your computer. Go to System. Go to Advanced system settings. Click Environment Variables. Look at the system variables box (the second box). Go down to the row that says 'Path'. Double click and go to the end of the line. Now you should have this: ....;C:\Program Files\MySQL\MySQL Server 5.1\bin\;C:\php\ In that order. If you have not got the Mysql here or Php here then they wont send information to each other. It must be in this order mysql first then the php bit. Then you must restart your computer for the effect to take place. If this fails, make sure that mysql is running. Go to control panel. Then administrative tools. Then services. Then look for Mysql and check that it is running. Quote Link to comment https://forums.phpfreaks.com/topic/146939-new-to-php/#findComment-771439 Share on other sites More sharing options...
fry2010 Posted February 25, 2009 Share Posted February 25, 2009 If you have all this already, then go to your php ini file. Look for the list of extensions. Its the long list half way down that looks like this: ;extension=php_bz2.dll ;extension=php_curl.dll ;extension=php_dba.dll ;extension=php_dbase.dll ;extension=php_exif.dll ;extension=php_fdf.dll ;extension=php_gd2.dll ;extension=php_gettext.dll ;extension=php_gmp.dll ;extension=php_ifx.dll ;extension=php_imap.dll....... You need to find this line: extension=php_mysql.dll and un-comment it, i.e. remove this ';' from the start. Save the file. Then restart apache. Quote Link to comment https://forums.phpfreaks.com/topic/146939-new-to-php/#findComment-771441 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.