TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-12-2009, 04:09 PM   #1 (permalink)
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default 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?
9three is offline  
Reply With Quote
Old 02-12-2009, 04:20 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

In your __construct method, after $this->connect(); add $this->select();
Salathe is offline  
Reply With Quote
Old 02-12-2009, 04:25 PM   #3 (permalink)
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default

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.
9three is offline  
Reply With Quote
Old 02-12-2009, 05:05 PM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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.
Salathe is offline  
Reply With Quote
Old 02-12-2009, 05:10 PM   #5 (permalink)
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default

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}
9three is offline  
Reply With Quote
Old 02-12-2009, 10:18 PM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

You need to specify a database with mysql_select_db.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 02-12-2009, 11:08 PM   #7 (permalink)
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default

I am in my select method.
9three is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the Singleton design pattern Karl Advanced PHP Programming 27 10-22-2012 08:16 AM
Using the factory pattern (mad rantings of a mind without coffee) sketchMedia Advanced PHP Programming 35 09-25-2009 11:05 AM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
Adding Images to a database from a folder Rendair Advanced PHP Programming 3 01-13-2008 07:40 PM
Important Database Structure Question! AnthonyOS MySQL & Databases 5 12-20-2007 03:26 PM


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

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design