使用$ user_id检索和显示数据

问题描述 投票:-2回答:1

我的目的是当用户在feed-view.php页面上显示一个帖子时,在mysql数据库中的users表中显示用户生物数据的剪辑,就像在facebbok和twitter中使用$ user_id传递一样当用户发帖时,将消息发送到mysql db中的posts表。所以我正在尝试使用用户的$ user_id(谁发布帖子)从数据库中的users表中查询他们的生物数据。我希望显示一个图像和一些其他信息,如名称,phonenumber等,还要创建一种循环,以便后续帖子采用相同的模板或结构。我在下面设计的代码失败了。我需要帮助。

其次,我尝试使用会话变量将数据从users表传输到posts表,以获取在发布帖子时显示的已登录用户的生物数据,但是我在转移包含图像的会话变量时遇到了困难(jpeg )例子是$ image = $ _SESSION ['image'];当我尝试将值传输到posts表以显示在feeds-view页面时,它显示“Error您的SQL语法中有错误;请检查与您的MariaDB服务器版本对应的手册,以便在附近使用正确的语法” 'id ='W5M0MpCehiHzreSzNTczkc9d'?>“

目标:显示撰写帖子的用户或其帖子正在feed-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>
php mysql
1个回答
0
投票

可能你在MariaDB上遇到了这个错误,因为你错了这个行(你发送MariaDB来按空字段排序行):

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

阅读你的问题,我想你想按“id”字段排序所有行,所以你必须为这一行替换错误的行:

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

这样,MariaDB就不会抛出这个错误,你可以继续发展你的想法

© www.soinside.com 2019 - 2024. All rights reserved.