Click here to Skip to main content
15,900,390 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
class model
{
public function sel($con,$t1)
{
$sql="select * from $t1";
$q=$con->query($sql);
while($fet=$q->fetch_object())
{
$res[]=$fet;
}
return $res;
}

What I have tried:

i was try submit data in database....
Posted
Updated 1-Mar-17 1:52am
Comments
Richard MacCutchan 1-Mar-17 5:25am    
At a guess your database query did not return any values. Check the documentation for possible causes of the error(s).

1 solution

You have to check if the query was successful. See PHP: mysqli_result::fetch_object - Manual[^].

The second error informs you that the $res variable has not been defined when used in the return statement. It has been defined in the loop but is out of scope (not seen outside the loop). So you have to define it first in the scope of the function.

So use something like (untested):
PHP
public function sel($con,$t1)
{
    $res = array();
    $sql="select * from $t1";
    $q=$con->query($sql);
    if ($q)
    {
        while ($fet=$q->fetch_object())
        {
            $res[]=$fet;
        }
    }
    return $res;
}

Note also that $con must be a valid connection (check it before calling the function).
 
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