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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Issues with classes: mysqli_query() expects parameter 1 to be mysqli, null given

    Hello everyone!

    I am having trouble the last few days trying to output data from my database. This is concerning using the "mysqli_connect" and "mysqli_query" parameters.

    What I have done is I want to put the connection settings in a class, and I with the normal "mysql_connect" it does work. But with mysqli it doesn't work.

    What I get is an error message saying: "Warning: mysqli_query() expects parameter 1 to be mysqli, null given". Is there anything I am doing wrong here?


    PHP Code:
    class database_connect {
        var 
    $db_host;
        var 
    $db_user;
        var 
    $db_pass;
        var 
    $db_db;
        function 
    connect() {
            
    $connect_db mysqli_connect($this->db_host$this->db_user$this->db_pass$this->db_db) or die ("Connection failed");
        }
        function 
    close() {
            
    mysqli_free_result($result);
            
    mysqli_close($connect_db);
        }
    }

    $db_countries = new database_connect;
    $db_countries->db_host "localhost";
    $db_countries->db_user "root";
    $db_countries->db_pass "";
    $db_countries->db_db "site_countries";

    function 
    dbconnect_countries() {
        global 
    $db_countries$connect_db;
        
    $db_countries->connect();
        
    $query "SELECT * FROM countries";
        
    $result mysqli_query($connect_db$query) or die ("Error in query: $query. ".mysqli_error());
        if (
    mysqli_num_rows($result) > 0) {
            while(
    $row mysqli_fetch_row($result)) {
                echo 
    '<div>'.$row[0].'</div>';
            }
        }
        else {
            echo 
    '<div>No countries found!</div>';
        }
        
    $db_countries->close();


    Any help would be appreciated!

    Elmo

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    You're not really displaying an understanding of variable scope. You should avoid trying to global scope variables and connection pointers. You also don't seem to understand how classes work. You know you can use mysqli as an object itself?

    When you call a new instance of a class, the object will call the construct() magic method. I would use this to connect to the database, or at least set the connection parameters (although an immediate connection avoids actually storing the values). I would then store the connection pointer in a property of the class. The class should also store methods to act on the connection.

    Classes and objects are used to handle specific areas - separating logic into classes that deal with similar tasks. Therefore, a database class should be built to contain all the tools to access the database.

    How I would build your database class:
    PHP Code:
    class database {

        private 
    $connection '';

        protected function 
    __construct($host$db_user$db_pass$database){
            
    $this->connection mysqli_connect($host$db_user$db_pass$database);
            if(!
    $this){
                throw new 
    Exception('Connect failed: '.mysqli_error());
            }
        }

        protected function 
    query($query){
            
    // Could probably throw an exception here as well, if result was false
            
    return mysqli_query($this->connection$query);
        }

        protected 
    __destruct(){
            
    mysqli_close($this->connection);
        }

    Google some database class examples - the php.net manual has a great explanation of PHP class definitions.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.


  •  

    Posting Permissions

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