I have this class

class mysql {
    public $dbname, $dbuser, $dbpass, $dbhost, $dbcon;
    public $auto_slashes;
    public $sql_error;
    public $row_count;
    public function __construct($dbn, $dbh, $dbu, $dbp) {
        $this->dbhost=$dbh;
        $this->dbname=$dbn;
        $this->dbpass=$dbp;
        $this->dbuser=$dbu;
        $this->auto_slashes = true;
        $this->doconnect();
          }
        public function doconnect()
        {
        $this->dbcon=mysql_connect($this->dbhost,$this->dbuser, $this->dbpass);
        mysql_select_db($this->dbname);
        }

but I'm getting this connection error, line 17 is the $this->dbcon=mysql_connect line:
<br />
<b>Warning</b>: mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Can't connect to MySQL server on '127.0.0.7' (60) in <b>/Users/jr/pear/share/pear/lib/db.class.php</b> on line <b>17</b><br />
<br />
<b>Warning</b>: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/Users/jr/pear/share/pear/lib/db.class.php</b> on line <b>55</b><br />
NULL

This is my php web page below:
Do you know why the error says 127.0.0.7 instead of 127.0.0.1? I am hiting the server remotely maybe that is why.
How can I fix the connection error. It creates the new $db object and it looks like it calls the constructor?
The database Server and web server are working before I changed it from procedural to object oriented.
TIA,

$dbhost="127.0.0.1:3306";
$dbuser="xxx";
$dbpass="xxxx";
$dbname="xxxx";
$db=new mysql($dbname, $dbhost, $dbuser, $dbpass);
require_once 'lib/global.inc.php';
$sql = 'SELECT todo,date,name FROM task WHERE todo > "" ';
if($db->get_row($sql)){
$result = $obj->get_row($sql,$type='MySQL_ASSOC');	
while($result_of_result = mysql_fetch_assoc($result))
	{
    	$arr_result[] = $result_of_result;
	}	

};

    As an aside; is there a particular reason why you're building an OO interface around the (outdated) MySQL extension instead of using the existing MySQLi OO interface?

    Have you tried just connecting to [font=monospace]localhost[/font]? If you haven't set [font=monospace]mysql.default_host[/font] to something else, then the driver will use [font=monospace]localhost:3306[/font].

    What I don't get is the same as you - where the connection is being changed (I think the (60) refers to a port number; but I don't use MySQL and its documentation doesn't say what it means.)

    Er, unless I misunderstand you when you say "I am hiting the server remotely maybe that is why." because if the database is on another server then its address wouldn't be 127.0.0.1.

      Weedpacket;11006889 wrote:

      As an aside; is there a particular reason why you're building an OO interface around the (outdated) MySQL extension instead of using the existing MySQLi OO interface?

      Is he actually doing that?

      jrough;11006879 wrote:

      in <b>/Users/jr/pear/share/pear/lib/db.class.php</b> on line <b>17

      And no, I don't believe that's saying something about the port number either, since a mysql_connect('127.0.01') gives me

      Warning: mysql_connect() [function.mysql-connect]: [2002] Operation timed out
      (trying to connect via tcp://127.0.0.7:3306) in
      

      which obviously isn't the same error message, so the actual error message syntax could differ. Still, I find it unlikely they'd show port number using :pnumber in one place and (pnumber) in another.

        Some hosts need to know a remote connection will be coming in before they let you in. The remote connection would be binded from your User panel before you use it. Otherwise there would be a lot more hacking of database servers!

          johanafm;11006923 wrote:

          Is he actually doing that?

          jrough wrote:

          in <b>/Users/jr/pear/share/pear/lib/db.class.php</b> on line <b>17

          Yes, but the code posted refers to a class "mysql", and that's not the PEAR class there. On the other hand, the PEAR DB class is itself deprecated; so either way, why?

          johanafm wrote:

          Still, I find it unlikely they'd show port number using [noparse]:p[/noparse]number in one place and (pnumber) in another.

          So do I to be honest (which raises the question why they'd show the port number in one connection error message (2002) and not in another (2003).) - it's just that the first thing I noticed was that 7 == 1 + (3306 % 60); MySQL's documentation only says that the thing in parentheses is a number, and I didn't really need the documentation for that - and I'm not inclined to go trawling through the source code to find out where it comes from.

          I still want clarification whether (a) using "localhost" instead of "127.0.0.1" makes a difference, and (b) whether the "remote server" really is on "127.0.0.1".

          There's another bug coming up after this one is fixed:

          if($db->get_row($sql)){
          $result = $obj->get_row($sql,$type='MySQL_ASSOC');    
          while($result_of_result = mysql_fetch_assoc($result))

          Where is $obj defined, and why is it necessary to fetch a result from a result? Surely the whole point of using a DB abstraction layer would be to avoid using functions like mysql_fetch_assoc()?

            Regarding error #2:Sorry, I was going to create a new db.class.php class and call it object but the class already does that and names it $db so I fixed that error, it should be $db not $obj, but are you all saying that I shouldn't use this class because it uses mysql_connect() function which is old. I should scrap this whole class and just use mysqli instead? I think that makes sense.

            About the connection error: It does work with a regular connection without using the db.class.php. This connection works:

            $dbname= "taskos";
            $link = mysql_connect('127.0.0.1:3306', 'xxx', 'xxxx');
            mysql_select_db($dbname);

            So it is strange I get this error and it changes it to 127.0.0.7 using this class.

            Is it because it is calculating the port number as a string? I need the port to connect using TCP that is why I put the port number. I use this server as a test server for remote connections to demo files to clients. I have it on the outside IP outside the firewall. It is not secure but it only has test data. I'm going to scrap it and use mysqli
            tnx,

            class mysql {
            public $dbname, $dbuser, $dbpass, $dbhost, $dbcon;
            public $auto_slashes;
            public $sql_error;
            public $row_count;
            public function __construct($dbn, $dbh, $dbu, $dbp) {
            $this->dbhost=$dbh;
            $this->dbname=$dbn;
            $this->dbpass=$dbp;
            $this->dbuser=$dbu;
            $this->auto_slashes = true;
            $this->doconnect();
            }
            public function doconnect()
            {
            $this->dbcon=mysql_connect($this->dbhost,$this->dbuser, $this->dbpass);
            mysql_select_db($this->dbname);
            }
            public function runquery($sql)
            {
            $result=mysql_query($sql);

            return $result;

                }
              Write a Reply...