Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4119916
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:09:11+00:00 2026-05-20T23:09:11+00:00

I am trying to build a self-hosted PHP site, but I am having trouble

  • 0

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


?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:09:11+00:00Added an answer on May 20, 2026 at 11:09 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build site using OOP in PHP. Everyone is talking about Singleton,
Im trying to build a calculator with PyQt4 and connecting the 'clicked()' signals from
I'm trying to build a class that inherits methods from Python's list, but also
I'm trying to build a WCF self hosted service (eventually in a windows service)
I'm trying build a method which returns the shortest path from one node to
I'm trying build an App Engine connected Android application and am having some problems
I'm trying build a MVC framework, but I'm confused about manage themes. Well... I
I am trying to build a self-updating collection. Each item in the collection has
I'm trying to build a sfx (self extracting archive) using winrar. I'm using VS2008
I'm trying to build a video thumbnailer using gst-python, it looks like this. from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.