0

I'm trying to learn OOP and i'm trying to create a class which will select me info from a table and a line that i choose. (trying my best to explain it good, not sure if i'm doing very well tho)

This is my code in the class:

class SelectQuery {

    public function db_select($query_table, $query_extra) {

        //$result = mysqli_query($conn, "SELECT * FROM `ideas` WHERE `Accepted` = 1 ORDER BY RAND() LIMIT 1");
        //$query = 'SELECT * FROM '.$query_table.' '.$query_extra.'';
        //$results = $this->con->query($query);

        $this->con = mysqli_connect('localhost', 'root', '', 'forum');

        $results = $this->con->prepare('SELECT * FROM '.$query_table.' '.$query_extra.'');
        $results->execute();

        return $results;
    }
}

And this is the code in the actual page:

include_once 'Classes/SelectQuery.php';

$SelectQuery = new SelectQuery();

$query_table = 'category';
$query_extra = '';

$SelectQuery->db_select($query_table, $query_extra);

$date = $db_select->$result;

$date = mysqli_fetch_assoc($result);

echo $date;

while($categories = $result->fetch_assoc())
{
    echo '<p>';
    echo $categories['ID'].' ';
    echo $categories['Name'].'</p>';

    while($forums = $result_forum->fetch_assoc())
    {
        echo $categories['Name'].'</p>';
    }
}

The problem isnt in the class(i think), it's in the actual page when i'm trying to get the info that i want, there is no error until here:

$SelectQuery->db_select($query_table, $query_extra);

but after that line when i'm trying to really get the info there is a lots of errors and i'm probably doing it wrong... i would like to know how can i fix it, thanks :)

4
Contributors
4
Replies
43
Views
5 Months
Discussion Span
Last Post by JamesCherrill
0

In fact you call basic function which contained in to the object declaration.
I wold recommend write database class which is extended mysqli. In connstructor make connection, in destructor call disconnect then add your "db_select" as public function eg

class DataBase extends mysqli {
    public function __construct(){
        parent::init();
        parent::options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 1');
        parent::real_connect(DBhost, DBuser, DBpass, DBname, DBport)
    }
    public function __destruct(){
        $this->close();
    }
    public function db_select($sql, $inParam, &$outParam){
        $stmt = $this->stmt_init();
        $stmt->prepare($sql);
        $stmt->bind_param('s', $inParam);
        $stmt->execute();
        $stmt->bind_result($col1, $col2, $col3);
        while($stmt->fetch()){
            $outParam[] = array($col1, $col2, $col3);
        }
        $stmt->free_result();
        $stmt->close();
    }
}

then you can make connection, repeatedly call up a variety of methods and call disconnection at the end eg

$db = new DataBase(); // make connection

$db->db_query(/* add method params */);
$db->db_query(/* other SQL query */);
$db->db_query(/* and other SQL query */);

$db = NULL; // call destructor for disconnect from DB

Edited by AndrisP

0

Maybe a small point, but naming is ever so important in OO implementations... Would that class not be better called "Connection"?

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.