Mysql Connection Failure

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alienmonkey
Forum Newbie
Posts: 2
Joined: Sun Jul 24, 2011 1:45 am

Mysql Connection Failure

Post by alienmonkey »

Hi,

I'm a newbie to php, what's suppose to happen when the main page loads is a connection is to be made to the database server. The issue is it doesn't happen, $link returns null causing the connection not to be established. What am I missing that is making the connection fail?

These are functions part of a class

Code: Select all

         /**
         * Constructor
         *
         * @param string $host MySQL host address
         * @param string $user Database user
         * @param string $pass Database password
         * @param string $db_name Database name
         * @param boolean $persistant Is persistant connection
         * @param  boolean $connect_now Connect now
         * @return void
         */
        public function __construct($host, $user, $pass, $db_name, $persistant = true, $connect_now = true)
        {
                $this->host = $host;            // Host address
                $this->user = $user;            // User
                $this->pass = $pass;           // Password
                $this->db_name = $db_name;              // Database

                if ($connect_now)
                        $this->connect($persistant);

                return;

        }

         /**
         * Connect to the database
         *
         * @param boolean $persist Is persistant connection
         * @return boolean
         */
        public function connect($persist)
        {
                if ($persist)
                        $link = @mysql_pconnect($this->host, $this->user, $this->pass);

                else
                         $link = @mysql_connect($this->host, $this->user, $this->pass);

                //if (!$link)
                        //trigger_error('Could not connect to the database.', E_USER_ERROR);

                if ($link)
                {
                        $this->link = $link;
                        if ( @mysql_select_db($this->db_name, $link))

                                return true;
                }

               return false;

        }
This is what the code is returning.

Code: Select all

object(MySQLAdap)#2 (6) { ["host":"MySQLAdap":private]=> string(15) "192.168.1.1" ["user":"MySQLAdap":private]=> string(4) "user" ["pass":"MySQLAdap":private]=> string(8) "password" ["db_name":"MySQLAdap":private]=> string(8) "database" ["link":"MySQLAdap":private]=> NULL ["result":"MySQLAdap":private]=> NULL }
alienmonkey
Forum Newbie
Posts: 2
Joined: Sun Jul 24, 2011 1:45 am

Re: Mysql Connection Failure

Post by alienmonkey »

Issue solved

Had nothing to do with the script. Changed bind address in /etc/mysql/my.cnf to static ip of the server and the host in mysql.user table to static ip as well.
Post Reply