How can I get this mysqli database class?

I will go straight to the chase. All I can achieve at this point with this class is a database connection. I can not make a request. Can you show me how to get this job and / or show me how best to transcode it.

<?php
class database{

public $dbHost = '';
public $dbUser = '';
public $dbPass = '';
public $dbName = '';

public $db;

public function __construct(){}

public function dbConnect(){
    $mysqli = new mysqli($this->dbHost, $this->dbUser, $this->dbPass, $this->dbName);

    /* check connection */
    if (mysqli_connect_errno()){
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }else{
        echo 'connection made';
    }
    /* close connection */
    $mysqli->close();
}

public function query($sql){
    $query = $sql;
    self::preparedStatement($query);
}

public function preparedStatement(){
    if ($stmt = $mysqli->prepare($query)){

        /* execute statement */
        $stmt->execute();

        /* bind result variables */
        $stmt->bind_result($name, $code);

        /* fetch values */
        while ($stmt->fetch()) {
            printf ("%s (%s)\n", $name, $code);
        }

        /* close statement */
        $stmt->close();
    }
}

public function __destruct(){}

}
?>
+3
source share
2 answers

It worked for me. I commented on my changes.

<?php
class database{

public $dbHost = '';
public $dbUser = '';
public $dbPass = '';
public $dbName = '';

public $db;

public function __construct(){}

public function dbConnect(){
    ### not $mysqli
    $this->db = new mysqli($this->dbHost, $this->dbUser, $this->dbPass, $this->dbName);

    /* check connection */
    if (mysqli_connect_errno()){
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }else{
        echo 'connection made';
    }
    /* close connection */
    ### $this->db->close(); // DO NOT close the connection here!
}

public function query($sql){
    $query = $sql;
    self::preparedStatement($query);
}

public function preparedStatement($query){ ### parameter $query added

    if ($stmt = $this->db->prepare($query)){ ### not $mysqli->prepare()

        /* execute statement */
        $stmt->execute();

        /* bind result variables */
        $stmt->bind_result($name, $code);

        /* fetch values */
        while ($stmt->fetch()) {
            printf ("%s (%s)\n", $name, $code);
        }

        /* close statement */
        $stmt->close();
    }
}

public function __destruct(){}

}


### Test code
/*
$db = new Database();
$db->dbHost = '127.0.0.1';
$db->dbUser = 'root';
$db->dbPass = 'root';
$db->dbName = 'test';
$db->dbConnect();
$db->query('SELECT * FROM test');
*/
?>
+1
source

You omitted the parameter:

public function preparedStatement($query)

(and this method should actually be static)

Next time try debugging the code before requesting. Even simple instructions echowould do here.

EDIT:, . , . $mysqli , preparedStatement(), __construct() .

0

Source: https://habr.com/ru/post/1715608/


All Articles