Home fetch_assoc returns last inserted elements
Reply: 0

fetch_assoc returns last inserted elements

user2234
1#
user2234 Published in April 20, 2018, 10:47 am

For some reasons mysql returns only last inserted element in table,could anyone give a tip what's wrong with my code? i'm just practicing php and trying to figure out best ways to make functions easily reusable(just practicing oop i mean). i know this is a bad way but it's only for practice purposes.

<!DOCTYPE html>
<html>
<head>
    <title>Test Title</title>
</head>
<body>
    <?php 
        class Database{
            public $db;

            public function __construct($host,$username,$password,$dbname){

                $this->db=new MySQLi($host,$username,$password,$dbname);

            }
            public function getData(){
                $query="SELECT * FROM artisan";
                $result=$this->db->query($query);
                if($result->num_rows>0){
                    while($row=$result->fetch_assoc()){
                        echo "ID: ".$row["id"]. "-Username: ".$row["username"]. ", "."Email: ".$row["email"]."<br>";
                    }
                }else{
                    echo "No results found!";
                }

            }
            public function getContent(){
                $query="SELECT * FROM content";
                $result=$this->db->query($query);
                $values=array();
                if($result->num_rows>0){
                    while($row=$result->fetch_assoc()){
                    $values=array(
                        "title"=>$row['title'],
                        "body"=>$row['body']
                    );
                }

            }
            return $values;
            }
        }
        $database=new Database('localhost','root','','test');
        $database->getData();
        $values=$database->getContent();

     ?>

     <style type="text/css">
        .container{
            text-align:center;
        }
     </style>
     <div class="container">
      <?php

        for($i=0;$i<count($values);$i++){
        echo $values["title"]."<br>";
        echo $values["body"]."<br>";
      }
       ?>
     </div>
</body>
</html>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.316264 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO