donsiuch
donsiuch

Reputation: 493

MySQL failing to establish a connection using PHP

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,

Upvotes: 1

Views: 2965

Answers (3)

jexact
jexact

Reputation: 541

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

Upvotes: 1

C.A. Vuyk
C.A. Vuyk

Reputation: 1085

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);

Upvotes: 1

yejcob
yejcob

Reputation: 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();

Upvotes: -1

Related Questions