Jump to content

Adding database name within the initial parameters


9three

Recommended Posts

Hey,

 

What I'm trying to accomplish is add the database name within the first line to avoid having two lines. This is what I mean:

 

$connection = new mysql('localhost', 'root', '', 'cms');
$query = "SELECT ID, Username, Password, Email FROM admins";
$connection->query($query);

 

It's throwing me an exception saying that I did not select a database.

 

I'm trying to avoid this:

 

$connection = new mysql('localhost', 'root', '');
$connection->select(cms);
$query = "SELECT ID, Username, Password, Email FROM admins";
$connection->query($query);

 

Here is my class file:

 

  public function __construct($host = '', $username = '', $password = '', $dbname = '')
  {
    $this->host = $host;
    $this->username = $username;
    $this->password = $password;
    $this->dbname = $dbname;
    $this->connect();
  }
  
  public function connect()
  {
    $this->link = mysql_connect($this->host, $this->username, $this->password);
    if (!is_resource($this->link))
      Throw New Exception(mysql_error());
    else
      return $this->link;
  }
  
  public function select()
  {
    $this->select = mysql_select_db($this->dbname, $this->link);
    if (!is_resource($this->select))
      Throw New Exception(mysql_error());
    else
      return $this->select;
  }

 

I thought this was the way to do it but I guess I was wrong. Anyone know how to fix the parameters?

Link to comment
Share on other sites

Try:

  public function __construct($host = '', $username = '', $password = '', $dbname = '')
  {
    $this->host = $host;
    $this->username = $username;
    $this->password = $password;
    $this->dbname = $dbname;
    $this->connect();
    $this->select();//If I understand correctly (I don't use classes much yet), this will do it.
  }

Sorry for being dramatic before. Now I see where you are going with it, I think.

Link to comment
Share on other sites

Try:

  public function __construct($host = '', $username = '', $password = '', $dbname = '')
  {
    $this->host = $host;
    $this->username = $username;
    $this->password = $password;
    $this->dbname = $dbname;
    $this->connect();
    $this->select();//If I understand correctly (I don't use classes much yet), this will do it.
  }

Sorry for being dramatic before. Now I see where you are going with it, I think.

 

Doesn't work.

Link to comment
Share on other sites

There is a hint of information online I am still researching but it suggests that you can not return objects in your constructor. If this is true, you will need to call both connect() and select() outside of new mysql() and

your __construct() function

 

Does connect() work from the __construct?

Link to comment
Share on other sites

try this;

 

  public function __construct($host = '', $username = '', $password = '', $dbname = '')
  {
    $this->host = $host;
    $this->username = $username;
    $this->password = $password;
    $this->dbname = $dbname;
    $this->connect();
  }
  
  public function connect()
  {
    $this->link = mysql_connect($this->host, $this->username, $this->password);
    if (!is_resource($this->link))
      Throw New Exception(mysql_error());
    else {
     $this->select();
      return $this->link;
   }
  }
  
  public function select()
  {
    $this->select = mysql_select_db($this->dbname, $this->link);
    if (!is_resource($this->select))
      Throw New Exception(mysql_error());
    else
      return $this->select;
  }

 

If it doesn't work post the error message

Link to comment
Share on other sites

That puts me back to square one.

 

Fatal error: Uncaught exception 'Exception' with message 'No database selected' in C:\Users\9three\Desktop\Server\htdocs\gb\library\mysql.class.php:40 Stack trace: #0 C:\Users\9three\Desktop\Server\htdocs\gb\index.php(10): mysql->query('SELECT ID, User...') #1 {main} thrown in C:\Users\9three\Desktop\Server\htdocs\gb\library\mysql.class.php on line 40

Link to comment
Share on other sites

Why are you putting the select in a different function if you are not even providing the option to add a different database as a parameter?

 

<?php
class mysql {
  private $host, $username, $password, $dbname;

  public function __construct($host = '', $username = '', $password = '', $dbname = '')
  {
    $this->host = $host;
    $this->username = $username;
    $this->password = $password;
    $this->dbname = $dbname;
    $this->connect();
    $this->select();
  }
  
  public function connect()
  {
    $this->link = mysql_connect($this->host, $this->username, $this->password);

    if (!is_resource($this->link))
      Throw New Exception(mysql_error());

    return $this->link;
   
  }
  
  public function select($dbname='') {
    if (isset($dbname) && !empty($dbname)) {
         $this->dbname = $dbname;
    }

    $this->select = mysql_select_db($this->dbname, $this->link);
    if (!is_resource($this->select))
      Throw New Exception(mysql_error());
    else
      return $this->select;
  }
?>

 

Now this code:

 

$connection = new mysql('localhost', 'root', '');
$connection->select('cms'); // note quotes
$query = "SELECT ID, Username, Password, Email FROM admins";
$connection->query($query);

 

Should work. It should also work with the db being passed in as a parameter to the constructor.

Link to comment
Share on other sites

I am allowing the change of database in the parameters

 

//host, username, password, database name
public function __construct($host = '', $username = '', $password = '', $dbname = '')

 

//host = localhost, username = root, password = blank, database = cms
$connection = new mysql('localhost', 'root', '', 'cms');

Link to comment
Share on other sites

I am allowing the change of database in the parameters

 

//host, username, password, database name
public function __construct($host = '', $username = '', $password = '', $dbname = '')

 

//host = localhost, username = root, password = blank, database = cms
$connection = new mysql('localhost', 'root', '', 'cms');

 

Right, but you were using the $connection->select and trying to pass a parameter that way, which it does not accept a parameter.

Link to comment
Share on other sites

I don't want to use the object of $connection->select(); I want to avoid that.

 

I'm trying to execute connecting and selecting the database when I first initiate the object.

 

In the revised code I posted that is done. I added functionality to the select(). But if you call connect() it does it automatically. However if you want to change the DB at any time you just call $connection->select('newdbhere');

 

Look over my revision to see the changes.

 

$connection = new mysql('localhost', 'root', '', 'cms');
$query = "SELECT ID, Username, Password, Email FROM admins";
$connection->query($query);

 

Should work, given the code I modified above.

Link to comment
Share on other sites

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.