简体   繁体   中英

retrieve and display data using $user_id

My aim is to display a clip of a user's bio data from the users table in mysql database whenever a user makes a post to be diplayed on a feeds-view.php page just as is obtainable in facebbok and twitter using the $user_id passed along with the message to the posts table in mysql db when a user makes a post. So I'm trying to use the $user_id of the user(who made the post) to query their bio data from the users table in the database. I'm looking to display an image and a few other info such as name, phonenumber etc. and also to create a kind of loop so subsequent posts assume same template or structure. The code I devised below failed. Please I need help on this.

secondly, I attempted using session variables to transfer data from users table to posts table as a way to obtain bio data of a signed in user to be displayed when a post is made but I was stymied at transfering a session variable containing an image(jpeg) example is $image = $_SESSION['image']; as I tried to transfer the value to posts table to be displayed in feeds-view page, it says "Error You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' id='W5M0MpCehiHzreSzNTczkc9d'?>"

Target: Is to display bio data (ie image, name etc) of the user who makes a post or whose post is being displayed on the feeds-view.php page and also to create a kind of loop so subsequent posts assume same template or structure.

            <?php
           session_start();
           if(!isset($_SESSION['username'])){
            header('location:landing page.php');


      include_once('server.php');
      //include_once('server2.php');

       function load_user_objects($user_id){

      $query = "select * from clientcompany where id = '$user_id'"; 

      $result = mysqli_query($conn,$sql);

      if (!$result){
      return "no user found";
     }
    return $result[0];
    }   
    }

    ?>



   <!DOCTYPE html>
    <html>
    <head>
    <title>W3.CSS</title>
     <meta name="viewport" content="width=device-width, initial-scale=1">

    </head>
    <body>

    <div>
     <?php
    include_once('server.php');
      $sql = "SELECT * FROM post ORDER BY DESC";
     $result = mysqli_query($conn,$sql);

      function load_user_objects($user_id){
       $query = "select * from users where id = '$user_id'"; 

      $result = mysqli_query($conn,$sql);

       if (!$result){
       return "no user found";
      }  
       return $result[0];
     }

    function do_news_feed($user_id){
        $status_objects = $this->get_status_objects($user_id);

    foreach($status_objects as $status){?>
    <div>
     <?php $users = $this->load_user_objects($status->user_id);?>



    <?php       //this attempts to display foto of a user who makes a           post. Image accessed from users table.
       include_once('server.php');
       function getimage($user_id){ 
       $query = "SELECT image FROM users WHERE id = '$user_id'";  
         $result = mysqli_query($conn,$query);
        while($row = mysqli_fetch_array($result))
     {
      echo $users->'<img src="data:image/jpeg;           base64,'.base64_encode($row['image']).'"class="w3-circle" alt="userfoto"     

        style="width:100px; height: 100px; margin-top: 70px; margin-left:             20px">';
       }
       }

       ?>

         <?php // this attempts to access the users table to display data              using the load_user_objects function
         echo $users->name;
         ?>
         <?php
         echo $users->location[posts table][1];
         ?>  


          <?php // this attempts to access the posts table to display data.
         echo $status->title;
          ?>
          <?php 
        echo $status->message;
         ?>

        </div>
        <?php
         }
          }
          ?>
         </div>
        </body>
       </html>

Probably you get that error on MariaDB because you have this line wrong (you send MariaDB to order the rows by an empty field):

$sql = "SELECT * FROM post ORDER BY DESC";

Reading your question, I supposed you want to order all the rows by "id" field so you must replace the wrong line for this one:

$sql = "SELECT * FROM post ORDER BY id DESC";

That way MariaDB won't throw that error and you can keep developing your idea

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM