TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Adding database name within the initial parameters (http://www.talkphp.com/absolute-beginners/3971-adding-database-name-within-initial-parameters.html)

9three 02-12-2009 04:09 PM

Adding database name within the initial parameters
 
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:

PHP Code:

$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:

PHP Code:

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

Here is my class file:

PHP Code:

  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?

Salathe 02-12-2009 04:20 PM

In your __construct method, after $this->connect(); add $this->select();

9three 02-12-2009 04:25 PM

I just tried that and get a fatal error:

Code:

Fatal error: Uncaught exception 'Exception' in C:\Users\9three\Desktop\Server\htdocs\gb\library\mysql.class.php:31 Stack trace: #0 C:\Users\9three\Desktop\Server\htdocs\gb\library\mysql.class.php(15): mysql->select() #1 C:\Users\9three\Desktop\Server\htdocs\gb\index.php(8): mysql->__construct('localhost', 'root', '', 'cms') #2 {main} thrown in C:\Users\9three\Desktop\Server\htdocs\gb\library\mysql.class.php on line 31
Not sure what is the error as it doesn't really say much.

Salathe 02-12-2009 05:05 PM

The error is very clear, an Exception has been thrown but you have nothing in place to catch it. Wrap your code (not the class) in a try/catch block and see which exception is being thrown:
PHP Code:

try
{
    
$connection = new mysql('localhost''root''''cms');
    
$query "SELECT ID, Username, Password, Email FROM admins";
    
$connection->query($query);  
}
catch(
Exception $e)
{
   echo 
$e->getMessage();


Since you were throwing exceptions, I figured you already knew how to catch them. :-!

9three 02-12-2009 05:10 PM

I was actually doing that as you replied. But I still get the same error:

Code:

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(11): mysql->query('SELECT ID, User...') #1 {main}
Actually I had the method select() within the connect method and it was giving me the "no database selected". I just moved it within the constructor as you said, and i get:

Code:

exception 'Exception' in C:\Users\9three\Desktop\Server\htdocs\gb\library\mysql.class.php:31 Stack trace: #0 C:\Users\9three\Desktop\Server\htdocs\gb\library\mysql.class.php(15): mysql->select() #1 C:\Users\9three\Desktop\Server\htdocs\gb\index.php(9): mysql->__construct('localhost', 'root', '', 'cms') #2 {main}

Wildhoney 02-12-2009 10:18 PM

You need to specify a database with mysql_select_db.

9three 02-12-2009 11:08 PM

I am in my select method.


All times are GMT. The time now is 09:55 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0