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.'');

        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 :)

It is not good idea because your db_select() function make new connection on each query

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::options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 1');
        parent::real_connect(DBhost, DBuser, DBpass, DBname, DBport)
    public function __destruct(){
    public function db_select($sql, $inParam, &$outParam){
        $stmt = $this->stmt_init();
        $stmt->bind_param('s', $inParam);
        $stmt->bind_result($col1, $col2, $col3);
            $outParam[] = array($col1, $col2, $col3);

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.