2

I am trying to build a self-hosted PHP site, but I am having trouble connecting to the database from my PHP script.

Please note the following

1). This code allows me to connect to MAMP from my terminus without problem so I know MySQL is working etc

mysql --host=127.0.0.1 --port=8889 --user=root -p 

2) When I tried to build a site on a remote hosted platform, this code allowed me to connect from my php script to MySQL on the remove server, so I know there is nothing wrong with this code per se, but it didn't work on my computer.

defined('DB_SERVER') ? null : define("DB_SERVER","host"); defined('DB_USER') ? null : define("DB_USER","username"); defined('DB_PASS') ? null : define("DB_PASS","password"); defined('DB_NAME') ? null : define("DB_NAME","photo_gallery"); 

3 I have been able to run a self-hosted wordpress site on my computer, but I didn't establish the database connection for that (it's somewhere in the code) so I don't what I'm doing wrong. That site is accessible at the following path. http://localhost:8888/wordpress/ even though it is connected at port 8889 (according to mysql)

4 MAMP tells me that I can connect to the database from my own scripts using this format

Host localhost Port 8889 User root Password root 

Therefore, I added this line

defined('DB_PORT') ? null : define("DB_PORT","8889"); 

to this group, like so

defined('DB_SERVER') ? null : define("DB_SERVER","host"); **defined('DB_PORT') ? null : define("DB_PORT","8889");** defined('DB_USER') ? null : define("DB_USER","username"); defined('DB_PASS') ? null : define("DB_PASS","password"); defined('DB_NAME') ? null : define("DB_NAME","photo_gallery"); 

but it still didn't work. I'm being told database connection fails when I try to test it.

Any ideas?

EDIT. I tried to put the port next to the localhost but it's not working.

defined('DB_SERVER') ? null : define("DB_SERVER","localhost:8889"); defined('DB_USER') ? null : define("DB_USER","root"); defined('DB_PASS') ? null : define("DB_PASS","root"); defined('DB_NAME') ? null : define("DB_NAME","photo_gallery"); 

EDIT. The above code was in the config.php which was included into the database.php which is this

<?php require_once("config.php"); class MySQLDatabase { private $connection; function __construct(){ $this->open_connection(); } public function open_connection(){ $this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS); if(!$connection){ die("Database connection failed:" . mysql_error()); } else { $db_select = mysql_select_db(DB_NAME, $this->connection); if (!$db_select) { die("Database selection failed: " . mysql_error()); } } } public function close_connection(){ if(isset($this->connection)){ mysql_close($this->connection); unset($this->connection); } } public function query($sql) { $result = mysql_query($sql, $this->connection); $this->confirm_query($result); return $result; } public function mysql_prep( $value ) { $magic_quotes_active = get_magic_quotes_gpc(); $new_enough_php = function_exists( "mysql_real_escape_string" ); // i.e. PHP >= v4.3.0 if( $new_enough_php ) { // PHP v4.3.0 or higher // undo any magic quote effects so mysql_real_escape_string can do the work if( $magic_quotes_active ) { $value = stripslashes( $value ); } $value = mysql_real_escape_string( $value ); } else { // before PHP v4.3.0 // if magic quotes aren't already on then add slashes manually if( !$magic_quotes_active ) { $value = addslashes( $value ); } // if magic quotes are active, then the slashes already exist } return $value; } private function confirm_query($result){ if(!$result){ die("Database query failed:" . mysql_error()); } } } $database = new MySQLDatabase(); ?> 
4
  • I'd need to see more code to help you any further. Assuming the script is doing mysql_connect(DB_SERVER, DB_USER, DB_PASS); at a later stage, I'm not sure what would be wrong. For the sake of it, try removing the ternary portion and just making the lines like define("DB_SERVER","localhost:8889");. Commented Apr 2, 2011 at 2:43
  • @Dae, I removed the ternary part and it didn't change anything. Database connection failed. That error message is coming from the database.php file (now copied into OP). I set the constants in a config.php file that was included into database.php. Commented Apr 2, 2011 at 2:54
  • @Dae, thanks for your help. there was an error in my code. Commented Apr 2, 2011 at 3:11
  • No problem, glad you got it sorted. Commented Apr 2, 2011 at 12:52

5 Answers 5

3

What is the output of the following snippet?

<?php // test.php $host = 'localhost:8889'; $username = 'root'; $password = 'root'; $database = 'your_database'; if(!$connection = mysql_connect($host, $username, $password)) die('Error connecting to '.$host.'. '.mysql_error()); if(!mysql_select_db($database)) die('Error selecting '.$database.'. '.mysql_error()); echo 'Connection successful.'; ?> 

If this executes without an error, then you're probably just setting your constants incorrectly. Note that mysql_connect takes the port as part of the host parameter, not as a separate argument.

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

7 Comments

first of all, I note that when I put your code into my text editor, it suggested (without specifying) that there's an error of some sort in each of the "die" lines. when I ran the page, there was no output. Just a blank page. does that tell you anything?
There was a parse error initially, since edited, apologies. As a side note, you may want to turn error reporting on in php.ini for development.
o.k., maybe some progress. I tried your new version and it output "error selecting database," but nothing else. does that mean the connection still wasn't set?
o.k., wait, I commented out the first "die" and it gave me "Connection successful," but please note the following. When I amended my code to do defined('DB_SERVER') ? null : define("DB_SERVER","localhost:8889"); I still got a server error.
Do you mean second die? If your original script it still failing with the new host value, verify the database it is using exists. Add similar debugging to the mysql_ calls as used above.
|
1

try 127.0.0.1:3307

it worked for me

Comments

0

Can you try to use:

define("DB_SERVER","127.0.0.1");

1 Comment

didn't work. same result. The website encountered an error while retrieving localhost:8888/photo_gallery/index.php.
0

important: checking defined or not is done by defined() but not by comparing it to null so sample:

http://www.php.net/defined

defined('DBSERVER') or define('DBSERVER', 'localhost:8889'); 

providing port information while making connection is just to add it along with server name i.e if your mysql server is localhost then while connecting just make it

localhost:8889 

so your sample script may look like:

$strServer = "localhost:8889"; $strUser = "root"; $strPassword = "root"; $strDB = "test_db"; $conn = @mysql_connect($strServer, $strUser, $strPassword); if(!$conn) { die('Unable to connect to database: ' . mysql_error()); }else{ $db=mysql_select_db($strDB); if(!$db){die('Error Selecting Database: '.$strDB.' [Error : '.mysql_error().']');} } 

3 Comments

thanks. please see the bottom edit of the OP. I tried to implement the first four lines of your solution (i.e. putting localhost:8889) but I'm getting message that database connection failed.
by the way, my script wasn't comparing to null. it said that if the connection was already defined, then do null.
there is an error in line 15 or your database.php file. it should be if(!$this->connection) instead of if(!$connection)
0

Example with the default port 3306 of MySQL:

mysql_connect('localhost:3306', 'root', 'password');

This really makes things easier when copying scripts between the distant server and the local web server.

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.