2

I'm a complete beginner at this, but I've set up an EC2 server and I'm trying to develop a simple php-mysql based website.

I've managed to get apache2 running and I've got a domain pointed to the server. I'm trying to set up a very simple php script to attach to a database. The following script runs perfectly well when I use the command line php simple_script.php

<?php

echo "Hello\n";

$con = mysql_connect("localhost","james","<password>");
if($con){
echo "Connected\n";
}
?>

However, when I go to www.myurl.com/simple_script.php I only see the line "Hello". Any help would be gratefully received!

OK, so I've checked the apache error log, which is saying this: PHP Fatal error: Call to undefined function mysql_connect() in /var/www/www.myurl.com/simple_script.php on line 5 Any ideas how to resolve this?

3
  • What does the EC2's error log tell? Commented Feb 7, 2013 at 22:45
  • PHP via command line and via web browser may have different configurations, so maybe the extension mysql is not loaded for php web Commented Feb 7, 2013 at 22:46
  • 2
    Use mysqli/pdo - php_mysql is now officially deprecated Commented Feb 7, 2013 at 22:58

3 Answers 3

1

You're using a deprecated function. Use mysqli_* or look into the PDO class. http://php.net has a great documentation on the language :)

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I've tried changing the call to use mysqli_connect instead, it still works in the command line but not on the web! I'm thinking it must be a server configuration problem!
Try [ if (!$con) ] to be sure your not connected?
It worked after I restarted Apache, which seems a bit strange, I should probably have tried that sooner, cheers.
Ive actually had that issue before.
1

Add:

else {
    die(mysql_error());
}

To figure out the error

Comments

0

If you're trying to figure things out (and if this isn't on a production server), I'd start by displaying php errors by enabling error reporting in your php.ini file. To find out where php.ini is located, create a file that uses the phpinfo() function, and you should get the path. (This will also show you if mysql is running.)

<?php phpinfo();?>

In php.ini, look for the "error_reporting" line and set it to something like:

error_reporting = E_ALL & ~E_NOTICE

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.