using Mysqli within CLASS

Asked

Viewed 504 times

0

Hello, I’m in trouble, I have some queries to do, I wanted to leave within class, I’m making the following code:

<?php
class MySQL {
    private $user;
    private $password;
    private $database;
    private $host;

    public function __construct($user, $password, $database, $host="localhost"){
        $this->user     =   $user;
        $this->password =   $password;
        $this->database =   $database;
        $this->host     =   $host;

        $this->mysqli = new mysqli($this->host, $this->user, $this->password, $this->database);
        if (mysqli_connect_errno()) {
            die('FATAL ERROR: Can not connect to SQL Server.');
            exit();
        }
    }

    public function _query($qr)
    {
        $this->result = $this->mysqli->query($qr);
        return $this->result;
    }

    public function _close()
    {
        $this->mysqli->close();
    }
}

To call, I do:

    <?php
        session_start();
        $connect = new MySQL(UserMySQL, PassMySQL, DataBaseMySQL, ServerMySQL);
function test(){
        $search =   $connect->_query("SELECT value FROM tab2 WHERE type='".$type."'");
        $search =   $connect->_query("SELECT * FROM tab1 WHERE urlName='".$subName."'");
    }
    test();

But returns me error. Call to a Member Function _query() on a non-object in ..........

  • 1

    From the error message, it seems that the connection failed, from a var_dump($connect);

  • @rray Object(Mysql)#1 (5) { ["user":"Mysql":private]=> string(15) "user" ["password":"Mysql":private]=> string(12) "password" ["database":"Mysql":private]=> string(15) "database" ["host":"Mysql":private]=> string(9) "localhost" ["mysqli"]=> Object(mysqli)#2 (19) { ["affected_rows"]=> int(0) ["client_info"]=> string(6) "5.5.49" ["client_version"]=> int(50549) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["Errno"]=> int(0) [""""]=> string(0) "["error_list"]=> array(0) { } ["field_count"]=> int(0) ["host_info"]=>string(25) "Localhost via UNIX socket" ["info"]=> .....................

  • @rray ............................ NULL ["insert_id"]=> int(0) ["server_info"]=> string(10) "5.5.50-cll" ["server_version"]=> int(50550) ["stat"]=> string(155) "Uptime: 415835 Threads: 13 Questions: 1647446872 Slow queries: 178 Opens: 3500320 Flush Tables: 1 Open Tables: 2750 Queries per Second avg: 3961.780" ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(6998409) ["warning_count"]=> int(0) } }

  • 1

    And I agree that the problem may be in connection. And to make it easier to detect errors, it would be better to leave only the instance of variables. Create a connection-only method and call it as soon as you instantiate the class.

  • 1

    Copy, paste, changed database and query data and returned no error, the problem seems to be elsewhere.

  • 1

    The test you did was the one you added to the edit? Because it won’t work that way

  • @rray and Deborah thank you very much for the tips, I managed to solve, and I have published explaining, thank you very much!!!!!

  • 1

    So it’s nice to always post the real code, sometimes hide some details in the question make it difficult to detect the problem.

  • @rray Truth... living and learning right? I won’t do it again.

Show 4 more comments

1 answer

2


Failed to declare variable $connect global within the function, thus:

function test(){
    global $connect;
    $search =   $connect->_query("SELECT value FROM tab2 WHERE type='".$type."'");
    $search =   $connect->_query("SELECT * FROM tab1 WHERE urlName='".$subName."'");
}

I ask forgiveness to the community for making this mistake, anyway, now whoever has this doubt will know hahaha

  • That’s right the problem was the scope of the variable. There is another way to solve this, pass the $connect in signing the function function test($connect){ ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.