0

This works in localhost on my PC, as well as localhost on the remote server:

connection.php

<?php
    $server   = "localhost";
    $database = "somedatabase";
    $username = "someuser";
    $password = "somepassword";

 $mysqlConnection = mysql_connect($server, $username, $password);
    if (!$mysqlConnection)
        {
            echo "Please try later.";
        }
    else
        {
            mysql_select_db($database, $mysqlConnection);
        }
?>

However, while this works in localhost on my PC, it doesn't work on localhost on the remote server (db.php and connection2.php are in the same folder). I get "Please try later" echoed out.

db.php

<?
    $server   = "localhost";
    $database = "somedb";
    $username = "someuser";
    $password = "somepass";
?>

connection2.php

<?php
    require("db.php");

    $mysqlConnection = mysql_connect($server, $username, $password);
        if (!$mysqlConnection)
            {
                echo "Please try later.";
            }
        else
            {
                mysql_select_db($database, $mysqlConnection);
            }
?>

Thoughts?

Update: Per CharlesRojas' suggestion, I tried echoing out the variables in connection2.php but nothing got echoed out. So, connection2.php is not reading the contents in db.php.

Update 2: I've added the solution below and will mark it as correct when I am able.

thanks_in_advance
  • 2,603
  • 6
  • 28
  • 44

3 Answers3

1

Have you tried echo getcwd();? That returns the current working directory that's it's looking for db.php in. Also, how are the permissions set for db.php? What is the exact error it's returning?

PS: The mysql functions (mysql_connect, mysql_select_db) are depricated, you should use mysqli or PDO unless you're just messing around.

EDIT

The required file started with

When I changed it to

Still, it's not clear why this combination worked fine on localhost on my local PC, but not on the remote server.

Ah, I totally missed that. Sounds like your local php config has short tags enabled, while your server does not.

Captain Hypertext
  • 2,446
  • 4
  • 27
  • 36
  • good suggestion on getwd(). I tried it, it returned the expected, correct working directory (the same directory as `connection2.php` and `db.php`). The permissions for `connection2.php` and `db.php` are 644. Both are owned by the user www-data. Thanks for the note about mysqli. I was going to update it later. What I don't understand is why it all works on my local server on my PC, but not on the remote server. – thanks_in_advance Mar 08 '15 at 23:51
0

Ok, instead of creating a file with variable names, here's what I suggest you do: create a config.php!

Here's what the config.php should look like:

<?php
define('DB_USER','root');
define('DB_HOST','localhost');
define('DB_PASS','asecretpass');
define('DB_NAME','somethingalittlespecial');
?>

Here's what your file SHOULD look like now:

<?php
    require_once("config.php");

    $mysqlConnection = mysql_connect(DB_HOST, DB_USER, DB_PASS);
        if (!$mysqlConnection)
            {
                echo "Please try later.";
            }
        else
            {
                mysql_select_db(DB_NAME, $mysqlConnection);
            }
?>

Also, instead of using mysql, you should be using mysqli:

sudo apt-get install php5-mysql php5-mysqlnd
sudo service apache2 restart

Rename all mysql commands to mysqli (i.e. mysqli_select_db)

Sameer Puri
  • 987
  • 8
  • 20
  • For php5-mysql, the message is that I already have the latest package. For php5-mysqlnd , I get: `Some packages could not be installed. This may mean that you have requested an impossible situation or if you are using the unstable distribution that some required packages have not yet been created or been moved out of Incoming. The following packages have unmet dependencies: php5-mysql : Conflicts: php5-mysqlnd but 5.5.9+dfsg-1ubuntu4.6 is to be installed E: Unable to correct problems, you have held broken packages.` – thanks_in_advance Mar 08 '15 at 23:56
  • I did everything else you told me except install `php5-mysqlnd` ... still unable to read the **required** file (in the same folder). – thanks_in_advance Mar 09 '15 at 00:05
  • Ok, it may be better if I just help you with this in person, i.e. via SSH, if you are fine with that. How can I contact you? – Sameer Puri Mar 09 '15 at 00:06
0

The required file started with <?.

When I changed it to <?php , just like the referencing file started, it worked fine.

Still, it's not clear why this combination worked fine on localhost on my local PC, but not on the remote server.

thanks_in_advance
  • 2,603
  • 6
  • 28
  • 44
  • 3
    It's because of short open tags are set on one server, but not on the other. http://php.net/manual/en/ini.core.php and http://stackoverflow.com/q/2185320/ – Funk Forty Niner Mar 09 '15 at 02:44