views:

369

answers:

2

I'll cut right to the chase. All I can achieve at this point with this class is a database connection. I am unable to make a query. Can you show me exactly how to get this working and/or show me how to recode it in a better way.

<?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(){}

}
?>
A: 

You have omitted a parameter:

public function preparedStatement($query)

(and this method actually should be static)

Next time try to debug your code before asking. Even simple echo statements would have done here.

EDIT: and even that wouldn't work. I think you've misunderstood the concept of variable scope. $mysqli should be an instance variable of that class, because it wouldn't persist to preparedStatement() if you just set it in __construct() as a local variable.

Ignas R
Yes I understand there are some serious issues here. I think 3 hours counts as "trying to debug your code before asking".
A: 

This worked for me. I've commented 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');
*/
?>
Ignas R
Your awesome! Thank you so much. I made almost all of those changes during my debugging, too bad I didn't put them all together at once. Thanks.