Jump to content

Recommended Posts

Before adding my __construct my class was working fine, but I figured I would cut down some code by when I initiate the class, within the same parenthesis add the parameters as well.

 

index.php

  $connection = new mysql('localhost', 'root', '');
  $connection->select('cms');

 

Before I added my construct:

  $connection = new mysql();
  $connection->connect('localhost', 'root', '');
  $connection->select('cms');

 

Anyway, It's throwing me an exception:

 

Fatal error: Uncaught exception 'Exception' with message 'Unable to select database' in C:\Users\9three\Desktop\Server\htdocs\CMS\admin\includes\library\mysql.class.php:30 Stack trace: #0 C:\Users\9three\Desktop\Server\htdocs\CMS\admin\index.php(8): mysql->select('cms') #1 {main} thrown in C:\Users\9three\Desktop\Server\htdocs\CMS\admin\includes\library\mysql.class.php on line 30

 

As well as a MySQL error:

 

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in C:\Users\9three\Desktop\Server\htdocs\CMS\admin\includes\library\mysql.class.php on line 27

 

mysql.class.php

  public $host, $user, $password;
  public $link, $select, $query, $fetch;
  
  public function __construct($host, $user, $password)
  {
    $this->host = $host;
    $this->user = $user;
    $this->password = $password;
  }

  public function connect()
  {
    $this->link = mysql_connect($this->host, $this->user, $this->password);
    if (!$this->link)
    {
      Throw New Exception('Unable to connect');
    }
  }

  public function select($select)
  {
    $this->select = mysql_select_db($select, $this->link);
    if (!$this->select)
    {
      Throw New Exception('Unable to select database');
    }
  }

 

my method select was working fine before...

Link to comment
https://forums.phpfreaks.com/topic/140241-construct-giving-me-problems/
Share on other sites

It needs to know if it's connected or not if you don't plan on telling it to connect.

 

 

Something like (psuedo code, in the select function):

 

if(not connected) {

    connect;

}

 

Or, you could just change the construct to:

 

 

public function __construct($host, $user, $password)

  {

    $this->host = $host;

    $this->user = $user;

    $this->password = $password;

    $this->connect();

    $this->select();

  }

 

 

Sometimes 'lazy' connections are done on purpose though.  That way, the over head is smaller of an unused DB class.

It's all well and good that you're passing things into the constructor, but you're not actually connecting.  Try changing __construct() to:

  public function __construct($host, $user, $password)
  {
    $this->host = $host;
    $this->user = $user;
    $this->password = $password;
    $this->connect();
  }

 

Also, I'd suggest making the variables private, not public.

It needs to know if it's connected or not if you don't plan on telling it to connect.

 

 

Something like (psuedo code, in the select function):

 

if(not connected) {

    connect;

}

 

Or, you could just change the construct to:

 

 

public function __construct($host, $user, $password)

  {

    $this->host = $host;

    $this->user = $user;

    $this->password = $password;

    $this->connect();

    $this->select();

  }

 

 

Sometimes 'lazy' connections are done on purpose though.  That way, the over head is smaller of an unused DB class.

 

So are you saying that using $this->connect() in the constructor is the lazy way? Which way would be the not-lazy way?

Nooooo....

 

 

Lazily connection is a design term (sort of... more of a strategy or something).  The first time I saw it was in Zend_Db, but I bet it's been around a long time.

 

The concept of lazy connections basically is this:

 

-Tell the DB instance the data it knows to connect, but do not have it connect.

-When a query is run or something else requiring a database connection, then try to make the connection.

 

The reasoning behind it is that if the DB class is never used, the DB connection is never made, therefore [as much] time isn't wasted.

No, lazy connections are connections that are initialized on-demand.  Like, if you just instantiated a DB class, it wouldn't connect until you actually did your first operation, which could save some memory and resources.  Depends on what you're actually trying to do.

Normally I only connect to a database when i HAVE to.

 

So basically what you are saying is that if I do something like

 

public function select($select)

{

  $this->connect();

  $this->query = mysql_select_db($select, $this->link);

}

 

It would actually be better?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.