Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/146939-new-to-php/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/146939-new-to-php/#findComment-771424
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/146939-new-to-php/#findComment-771427
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/146939-new-to-php/#findComment-771433
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/146939-new-to-php/#findComment-771439
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/146939-new-to-php/#findComment-771441
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.