SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Include() Or require() Question

    I'm using a config.php file which i use in my database class to change the values however this warning...

    Warning: mysql_pconnect() [function.mysql-pconnect]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home/creative/public_html/demo1/classes/database.class.php on line 16
    makes be believe that the functions are not picking up the values in teh config file. Could someone explain if i am not doing something right?

    Thanks

    PHP Code:
    <?php
        ob_start
    ();
        
    session_start();
        

        
    // The $siteURL is the full URL of where the current site is...
        
        
    $site 'demo1';
        
        
        
    // Database username, host, password and name
        
    $default_dbname '*****';
        
    $dbhost 'localhost';
        
    $dbusername '*****';
        
    $dbpassword '*****';


    ?>
    PHP Code:
    <?php

    require ('../config.php');
        
        
        class 
    database
        
    {    
            
                        
            function 
    databaseConnection()
            {
                
                 
    $link_id '';
                
                
                
    $link_id mysql_pconnect($dbhost,$dbusername,$dbpassword);
                if (!
    $link_id){
                    
    $MYSQL_ERRNO 0;
                    
    $MYSQL_ERROR "Connection Failed to the host".$dbhost."";
                    return 
    0;
                }
                else if (!
    mysql_select_db($default_dbname)){
                    
    $MYSQL_ERRNO mysql_errno();
                    
    $MYSQL_ERROR mysql_error();
                    return 
    0;
                }
            }
            
            function 
    sql_error()
            {
                if(empty(
    $MYSQL_ERROR)){
                    
    $MYSQL_ERRNO mysql_errno();
                    
    $MYSQL_ERROR mysql_error();
                }
                return 
    "$MYSQL_ERRNO$MYSQL_ERROR;
            }
            
            
            function 
    siteurl()
            {
            
                return 
    $this->$site;
            
            }
            
            function 
    setSiteUrl($site)
            {
                
    $this->site $site;
            }
    }
    ?>

  2. #2
    SitePoint Member
    Join Date
    Sep 2005
    Location
    Poland
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To get this working you need to add this piece of code in "databaseConnection" function body:

    PHP Code:
    global $dbhost,$dbusername,$dbpassword,$default_dbname

  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)
    or instead of using global make them constants.

    Code php:
    define('SOMENAME', 'Some Value');
     
    function foo() {
        print SOMENAME;
    }
     
    foo();
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    When you use include or require, its a good habit to either use the full path or that its a path from the current directory.

    Instead of:
    require ('../config.php');

    Use:
    require './../config.php';
    Or
    require '/fullpath/to/the/file/config.php';

    If you do not do this, php will first look for the file in the global defined file areas, hence you will waste disk io looking in areas the file does not exist.

  5. #5
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To get this working you need to add this piece of code in "databaseConnection" function body:

    PHP Code:
    global $dbhost,$dbusername,$dbpassword,$default_dbname;
    I've tried the above but i keep getting the error if i use the code below.

    PHP Code:
    <?php

    require ('./../config.php');
        
        
        class 
    database
        
    {    
            
                        
            function 
    databaseConnection()
            {
                 global 
    $dbhost$dbusername$dbpassword$default_dbname;
                 
    $link_id '';
                
                
                
    $link_id mysql_pconnect($dbhost,$dbusername,$dbpassword);
                if (!
    $link_id){
                    
    $MYSQL_ERRNO 0;
                    
    $MYSQL_ERROR "Connection Failed to the host".$dbhost."";
                    return 
    0;
                }
                else if (!
    mysql_select_db($default_dbname)){
                    
    $MYSQL_ERRNO mysql_errno();
                    
    $MYSQL_ERROR mysql_error();
                    return 
    0;
                }
            }
    }

    ?>

  6. #6
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If a function is expecting parameters, don't you need to tell the function what parameters to expect?

    I'm still fairly new at custom class/function writing, so apologies if I'm off base.

    PHP Code:
    function databaseConnection($dbhost$dbusername$dbpassword$default_dbname)
    {
      
    //CODE

    No, I REALLY dislike having to use Joomla.

  7. #7
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    join the club i'm pretty new to. In this case i wouldn't as its just a database connection script with local parameters. Still don't understand why its not working though.

  8. #8
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    PHP Code:
    <?php

        
    require ('./../config.php');

        class 
    database
        
    {
            
    /*
            * Database Connection Variables
            */
            
    public dbHost "";
            public 
    dbUser "";
            public 
    dbPass "";
            public 
    dbName "";

            
    /*
            * Database Connection Function
            */
            
    function databaseConnection()
            {
                
    $link_id '';
                
    $link_id mysql_pconnect($this->dbHost,$this->dbUser,$this->dbPass);
                if (!
    $link_id)
                {
                    
    $MYSQL_ERRNO 0;
                    
    $MYSQL_ERROR "Connection Failed to the host" $this->dbHost;
                    return 
    0;
                }
                else if (!
    mysql_select_db($this->dbName))
                {
                    
    $MYSQL_ERRNO mysql_errno();
                    
    $MYSQL_ERROR mysql_error();
                    return 
    0;
                }
            }
        }

        
    /*
        * Set Database Variables In Class
        */
        
    $DB->dbHost $dbhost;
        
    $DB->dbUser $dbusername;
        
    $DB->dbPass $dbpassword;
        
    $DB->dbName $default_dbname;

        
    /*
        * Connect To Database
        */
        
    $DB->databaseConnection();

    ?>
    You can keep your config file the same.
    Kayzio - We don't hesitate, we accelerate.


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
  •