Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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:
PHP
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:
PHP
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:
PHP
$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 :)

What I have tried:

I tried a lots of things like i said, i'm pretty new at this and i have no idea why it's not working...
Posted
Updated 8-Dec-20 19:50pm
v2

1 solution

try rewrite this:

PHP
$SelectQuery->db_select($query_table, $query_extra);
$date = $db_select->$result;
$date = mysqli_fetch_assoc($result);


like

PHP
$date= mysqli_fetch_assoc($SelectQuery->db_select($query_table, $query_extra));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900