简体   繁体   English

使用 $user_id 检索和显示数据

[英]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.我的目标是在用户发布要在 feeds-view.php 页面上显示的帖子时,从 mysql 数据库中的用户表中显示用户生物数据的剪辑,就像在 facebbok 和 twitter 中使用传递的 $user_id 获得一样当用户发布帖子时,将消息发送到 mysql db 中的帖子表。 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.所以我试图使用用户(发布帖子的人)的 $user_id 从数据库中的用户表中查询他们的生物数据。 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'];其次,我尝试使用会话变量将数据从用户表传输到帖子表作为获取登录用户的生物数据的一种方式,以便在发布帖子时显示,但我在传输包含图像的会话变量时遇到了困难(jpeg ) 示例是 $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'?>"当我尝试将该值传输到要在提要视图页面中显示的帖子表时,它说“错误您的 SQL 语法有错误;请检查与您的 MariaDB 服务器版本相对应的手册,以获取在附近使用的正确语法” '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.目标:是在 feeds-view.php 页面上显示发布帖子或正在显示其帖子的用户的生物数据(即图像、姓名等),并创建一种循环,以便后续帖子采用相同的模板或结构体。

            <?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):可能您在 MariaDB 上遇到该错误,因为您有此行错误(您发送 MariaDB 以按空字段对行进行排序):

$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:阅读您的问题,我想您想按“id”字段对所有行进行排序,因此您必须为此行替换错误的行:

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

That way MariaDB won't throw that error and you can keep developing your idea这样 MariaDB 就不会抛出那个错误,你可以继续发展你的想法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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