SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Non-Member
    Join Date
    Oct 2008
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    unexplained Exception?

    Hey,

    I'm running this through my index page:

    PHP Code:
    <?php

    require('library/includes.php');

    error_reporting(E_ALL|E_STRICT);
    ini_set('display_errors''on');

    try
    {
      
    $connection = new mysql('localhost''root''');
      
    $connection->select('cms');
    }
    catch (
    Exception $e)
    {
      echo 
    $e;
    }

    ?>
    And it returns:

    Code:
    exception 'Exception' in C:\Users\9three\Desktop\Server\htdocs\gb\library\mysql.class.php:29 Stack trace: #0 C:\Users\9three\Desktop\Server\htdocs\gb\index.php(10): mysql->select('cms') #1 {main}
    It's not really telling me anything, or at least I can't decipher it properly.

    mysql.class.php:
    PHP Code:
      public function __construct($host ''$username ''$password '')
      {
        
    $this->host $host;
        
    $this->username $username;
        
    $this->password $password;
        
    $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($dbname)
      {
        
    $this->select mysql_select_db($dbname$this->link);
        if (!
    is_resource($this->select))
          Throw New 
    Exception(mysql_error());
        else
          return 
    $this->select;
      } 
    What I'm trying to do is connect to whatever database was provided in this object $connection->select()

  2. #2
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First off, are you running PHP5?

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Well you are checking for the wrong type, mysql_select_db() does not return a resource but a boolean. So you are always going to fire that exception.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    Non-Member
    Join Date
    Oct 2008
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    of course! I didn't even think about that! I changed it to

    PHP Code:
      public function select($dbname)
      {
        
    $this->select mysql_select_db($dbname$this->link);
        if (!
    $this->select)
          Throw New 
    Exception(mysql_error());
        else
          return 
    $this->select;
      } 
    And it works. Thank you.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •