Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Apr 2009
    Location
    Denmark <'3
    Posts
    44
    Thanks
    5
    Thanked 1 Time in 1 Post

    Question OO PHP and mysql help please :)

    Okay I've just started on some of my first OO PHP but I'm getting an mysql error all the time.

    The error is :
    Code:
    Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO)
    and it comes when this function is called:
    PHP Code:
       public function query($query){
           return 
    mysql_query($query);
       } 
    and the while document where this function lives in is :
    PHP Code:
    <?php 
    require_once("constants.php");
    class 
    DILOAD_db
    {
        
      public 
    $connection;         //The MySQL database connection
       
       /* Class construcror */
       
    private function MySQLDB(){
           
    /* Make connection to database */
           
    $this->connection mysql_connect(DB_HOSTDB_USERDB_PASS) or die(mysql_error());
           
    mysql_select_db(DB_NAME$this->connection) or die(mysql_error());
       }
       
    /* Function for a query */
       
    public function query($query){
           return 
    mysql_query($query) or die(mysql_error());
       }
         

     }
     
    /* Creates database connection and the object*/
     
    $database = new DILOAD_db;
     
     
    ?>
    And the file where I'm calling this in looks like this:
    PHP Code:
    <php
    require_once('database.class.php');
    class 
    DILOAD_news
    {
        public function 
    list_pages(){
            
    $query DILOAD_db::query("SELECT * FROM pages"); // <-- IT IS CALLED HERE! 
            
    while($page mysql_fetch_assoc($query)):
                echo 
    '<a href="./page.php?id='$page['id'] . '">' $page['name'] . '</a><br />';
            endwhile;
        }
    ?> 
    I have also tried to make the query without the function and just a regular mysql_query but it just gives the same error. The error is only when I'm doing that query and I'm 100% sure that the constants used for connection is correct and I have also tried to connect without constants.

    and I know its not the best code ever but its my start to OO PHP but I'd be happy to take any tips hehe

    btw ANY help is appreciated !

    Regards
    Last edited by diload; 02-01-2010 at 02:28 AM.

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    The error details are fairly self explanatory.

    Code:
    Access denied for user 'ODBC'@'localhost'
    The username or password appear to be incorrect.

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Firstly, you're using the class in two ways. You're creating a new class with the $database = new DILOAD_db; and then you're calling it with the static function DILOAD_db::query.

    You should pass the db object to your class such as adding a new __construct function to the class, where you pass the $db object, then run $db->query()
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #4
    New Coder
    Join Date
    Apr 2009
    Location
    Denmark <'3
    Posts
    44
    Thanks
    5
    Thanked 1 Time in 1 Post
    never mind found out that my constructor was named wrong ......
    sorry >.>

  • #5
    New Coder
    Join Date
    Apr 2009
    Location
    Denmark <'3
    Posts
    44
    Thanks
    5
    Thanked 1 Time in 1 Post
    And thanks btw


  •  

    Posting Permissions

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