1

Using ubuntu 12.04 64 bit on Lenovo t410. Using apache2 and Mysql 5.5 and attempting to connect via localhost

I am attempting to establish a connection to a database that I made on localhost. When the line of code is reached to establish a connection, it seems Mysql simply hangs, and there is no error message displayed after. I verified that an echo works immediately prior to the connection attempt. I know that apache2 server is working as I can access the index page and display my html form.

I have tried etc/mysql/my.cnf setting the bind address to localhost.

My line of code looks like:

// Attempts to establish connection to MySql server
$connection = mysql_connect("localhost","username","password");

// Prints error message if the connection to MySql fails
if (!$connection){
    die("Connection failed: " . mysql_error());
}

echo "Connection established.";

I tried the connection line with single quotes and with no semi-colon as well.

I am willing to post the contents of any configuration file I have if the error isn't syntax. I haven't done anything fancy to Ubuntu, everything is the default install. I am new to CS and especially databases, PHP, and networking. This is my little experiment that I am stuck on.

Any help is greatly appreciated. Thanks,

  • Don
5
  • Have you established whether mysql is running as service? Commented Jul 6, 2012 at 13:43
  • What happens if you add this to the beginning of your code: error_reporting(E_ALL | E_STRICT);? Commented Jul 6, 2012 at 13:51
  • Per PHP official doc, you should consider using MySQLi and mysqli_connect instead of mysql_connect. See php.net/manual/en/function.mysql-connect.php and php.net/manual/en/mysqli.construct.php. Commented Jul 6, 2012 at 14:16
  • @jexact When I add: error_reporting(E_ALL | E_STRICT); I didn't notice anything at first. To make sure my code was making it past this point I inserted an echo statement after that line but before establishing a connection. My echo statement printed, and the connection hung again. Was there something in particular you would expect me to see? Commented Jul 7, 2012 at 0:01
  • @jexact I added this line of code: ini_set('display_errors', 1); and finally got the error message: Fatal error: Call to undefined function mysql_connect() in /var/www/index.php on line 9 . I am unsure what this means though Commented Jul 7, 2012 at 0:38

3 Answers 3

1

Can it be, because there is no error message, that the connection IS established, but you didn't do anything with it? I mean, what is the rest of your code, is there after your code here something like:

mysql_select_db("database_name",$connection);
Sign up to request clarification or add additional context in comments.

2 Comments

Yes my code does have that line of code as well. I would post but I cannot seem to get my code in the format that I did in my original post. My guess would be that the connection established echo would hit if the connection was created.
Your comment: "I added this line of code: ini_set('display_errors', 1); and finally got the error message: Fatal error: Call to undefined function mysql_connect() in /var/www/index.php on line 9 . I am unsure what this means though"----- This indicates that the function is not available. Probably mysql is not running as service. You have to check your configuration.
1

After reading your last comment, it appears the mysql extensions are not being loaded. Have a look at your php.ini, uncomment the following line (remove the semicolon at the beginning of the line) and restart your apache:

extension=php_mysql.so

Make sure the extension exists in the php extensions directory.

Due to the fact that you are using MySQL version > 4.1.3 it is strongly recommended that you use the mysqli extension instead. Have a look at this: PHP: MySQL Overview

Comments

-1

try to set

$mysql_user = "your_username";
$mysql_pass = "your_password";
$mysql_server = "Servername";

$link = mysql_connect($mysql_server,$mysql_user,$mysql_pass);
if (!$link) {
    header('HTTP/1.1 500');
    exit();

2 Comments

How is that going to help? If anything, you're giving LESS information about the failure than the original code!
@user1506616 I inserted this code and added the closing angle bracket etc. for a controlled test and it appears that the 'if' statement is not reached.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.