[英]retrieve and display data using $user_id
我的目标是在用户发布要在 feeds-view.php 页面上显示的帖子时,从 mysql 数据库中的用户表中显示用户生物数据的剪辑,就像在 facebbok 和 twitter 中使用传递的 $user_id 获得一样当用户发布帖子时,将消息发送到 mysql db 中的帖子表。 所以我试图使用用户(发布帖子的人)的 $user_id 从数据库中的用户表中查询他们的生物数据。 我希望显示图像和一些其他信息,例如姓名、电话号码等,并创建一种循环,以便后续帖子采用相同的模板或结构。 我在下面设计的代码失败了。 请我需要这方面的帮助。
其次,我尝试使用会话变量将数据从用户表传输到帖子表作为获取登录用户的生物数据的一种方式,以便在发布帖子时显示,但我在传输包含图像的会话变量时遇到了困难(jpeg ) 示例是 $image = $_SESSION['image']; 当我尝试将该值传输到要在提要视图页面中显示的帖子表时,它说“错误您的 SQL 语法有错误;请检查与您的 MariaDB 服务器版本相对应的手册,以获取在附近使用的正确语法” 'id='W5M0MpCehiHzreSzNTczkc9d'?>”
目标:是在 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>
可能您在 MariaDB 上遇到该错误,因为您有此行错误(您发送 MariaDB 以按空字段对行进行排序):
$sql = "SELECT * FROM post ORDER BY DESC";
阅读您的问题,我想您想按“id”字段对所有行进行排序,因此您必须为此行替换错误的行:
$sql = "SELECT * FROM post ORDER BY id DESC";
这样 MariaDB 就不会抛出那个错误,你可以继续发展你的想法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.