treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Class variable help

  • I'm trying to write a database crud class to get familiar with classes more for OOP. The problem lies in the $_mysql variable. It's not being read by my read function. I'm not sure why, did I not declare something correctly? Here's what I have:


    I have the start of my crud using a prepared statement.

    class Crud {

    protected $_mysql;


    public function __construct($host, $username, $password, $db) {
    $this->_mysql = new mysqli($host, $username, $password, $db) or die('There was a problem connecting to the database');
    }

    public function read($tableName)
    {
    $parameters = array();
    $results = array();

    $stmt = $_mysql->prepare("SELECT * FROM $tableName") or die('Problem preparing query');
    $stmt->execute();

    $meta = $stmt->result_metadata();

    while ( $field = $meta->fetch_field() ) {

    $parameters[] = &$row[$field->name];
    }

    call_user_func_array(array($stmt, 'bind_result'), $parameters);

    while ( $stmt->fetch() ) {
    $x = array();
    foreach( $row as $key => $val ) {
    $x[$key] = $val;
    }
    $results[] = $x;
    }

    return $results;
    }


    }



  • well you can use var_dump function always to check the status of variable.

    if it's the object of mysqli then you are on right way or else check the mysqli doc and i would suggest to look at the concept of factory method and singleton method. it would help you much to complete what you really want to complete. :)

    and once you done with this class(and only after) look at ORM's. they are cool if you want to do something like this...but solve this typical problem that everyone who starts with oop has to face...