Rohan Kumar Shrestha
Rohan Kumar Shrestha

Reputation: 101

Display all content of DB table, and not just single row?

I want to display all the data of the table from the database.I am able to display a single row of data but I want to display all the content?

views.php

         <?php
            $objUser=new User();
            $id=isset($_SESSION['uid']) ? $_SESSION['uid'] : '';
            $objUser->setuid($id);
            $data_list=$objUser->getalluser();
            //$row=$objUser->getalluser();

            ?>


            <div id="page-wrapper">
                        <div class="row">
                            <div class="col-lg-12">
                                <h1 class="page-header"><i class="fa fa-cog fa-fw"></i><span class="blue-word">User Setting</span></h1>
                            </div>


                                    <!--<?php //echo "<img src=\"Uploads/{$data_list['admin_img']}\" alt='{$data_list['admin_img']}' class='img-circle'  "?>-->

                            <?php



                                echo $data_list['admin_name'];

                                ?>

user.class.php

<?php
class User extends Connection
{
    private $admin_id;
    private $admin_email;
    private $admin_username;
    private $password;
    private $ip_used;
    private $login_date;
    private $acces_level;
    private $admin_img;


    public function setuid($ud='')
    {
        return $this->admin_id=$ud;
    }

    public function getIp_used()
    {
        return $this->ip_used;
    }
    //public function setadmin_img($aimg='')
   // {
    //    $this->admin_img=$aimg;
    //}
    public function setIp_used()
    {
        $this->ip_used=$_SERVER['REMOTE_ADDR'];

    }
    public function getaccess_level()
    {
        return $this->access_level;
    }
    public function setaccess_level($al="")
    {
        $this->access_level=$al;
    }
    public function getAdmin_email()
    {
        return $this->admin_email;
    }
    public function setAdmin_email($adem='')
    {
        $this->admin_email=$adem;
    }
    public function getAdmin_username()
    {
        return $this->Admin_username;
    }
    public function getPassword()
    {
        return $this->password;
    }
    public function setAdmin_username($uname='')
    {
        $this->Admin_username=$uname;

    }
    public function setPassword($upass='')
    {
        $this->password=sha1($upass);
    }
    public function setlogin_date()
    {

        $this->login_date=date('y-m-d h:i:s');
    }
    public function setimg_name($imgn='')
    {
        $this->img_name=$imgn;
    }
    public function getalluser()
{
$this->sql="SELECT * FROM admin_data";
$this->res=mysqli_query($this->conxn,$this->sql) or die($this->error=mysqli_query($this->conxn));
$this->numRows=mysqli_num_rows($this->res);
    $this->arr=array();
    if($this->numRows>0)
    {
        $this->data=mysqli_fetch_assoc($this->res);
        array_push($this->arr,$this->data);
    }

    return $this->data;
    //return TRUE;
}

}

I wanted to display the whole content of table admin_data. I can do this using for loop and checking that the varible $i < $numrows but in this case i have no idea how to do it?

Upvotes: 0

Views: 46

Answers (2)

Aleksey Krivtsov
Aleksey Krivtsov

Reputation: 291

If you want to fetch whole result at once, you must use mysqli_fetch_all. In your case is:

$this->data = mysqli_fetch_all($this->res, MYSQLI_ASSOC);

Upvotes: 1

user5307594
user5307594

Reputation:

if you want to display all rows you must do:

foreach($yourRows as $row) {
echo $row;
}

And this is all you need to do ;)

Upvotes: 1

Related Questions