[英]How to fetch data from two tables based on session variable php?
Actually im trying to display data from 2 tables on database based on current session, basically, i had read many article and forum on how to solve this problem but i still facing them till now. 实际上,我试图根据当前会话显示数据库中2个表中的数据,基本上,我已经读过很多文章和论坛来解决此问题,但直到现在我仍然面对它们。 so those are data and code that already i did:
所以这些是我已经做过的数据和代码:
<?php
include('db.php');
$sql="SELECT*
FROM users
INNER JOIN details ON users.id= details.id LIMIT 1 WHERE users.id ='$loggedin_id' " ;
$result=mysqli_query($db,$sql);
if (!$result) {
printf("Error: %s\n", mysqli_error($db));
exit();
}
?>
so here, i got this error: "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 'WHERE users.id = '16'' at line 3". 所以在这里,我得到了这个错误:“错误:您的SQL语法有错误;请查看与您的MariaDB服务器版本相对应的手册,以在第3行的'WHERE users.id ='16'附近使用正确的语法”。 so if i only use this query:
因此,如果我仅使用此查询:
$sql="SELECT* FROM users INNER JOIN details ON users.id= details.id"
it will display all data in my database, but i only need data from current session. 它会显示数据库中的所有数据,但是我只需要当前会话中的数据。
here is my session.php file: 这是我的session.php文件:
<?php
include('db.php');
session_start();
$user_check=$_SESSION['sess_username'];
$ses_sql=mysqli_query($db,"select username,id from users where username='$user_check' ");
$row=mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);
$loggedin_session=$row['username'];
$loggedin_id=$row['id'];
$role = $_SESSION['sess_userrole'];
if(!isset($loggedin_session) && $role!="user"){
header('Location: index.php?err=2');
}
?>
this is my tables detail: table users table details 这是我的表格详细信息: 表格用户 表格详细信息
hope someone can help me, im new with my sql and php. 希望有人可以帮助我,我的SQL和PHP是新的。 tq
q
Change your query like that. 像这样更改您的查询。
$sql="SELECT*
FROM users
INNER JOIN details ON users.id= details.id WHERE users.id ='$loggedin_id' LIMIT 1" ;
I think your query should be like below: 我认为您的查询应如下所示:
$sql = "SELECT * FROM users
INNER JOIN details ON users.id= details.id WHERE users.id = $loggedin_id ";
You can neglect the INNER JOIN and Use Procedural Steps as follows: 您可以忽略INNER JOIN并按以下步骤使用程序步骤:
$_session["anything"] = mysqli_insert_id(your db connection variable);
then store this session Id to a variable (like - $sessionCheck = $_session["anything"];
Insert the value of this variable to all the column in your Database on the session column as you perform insert. Let the session column by int and unique. 然后将此会话ID存储到变量中(例如-
$sessionCheck = $_session["anything"];
在执行插入操作时,将此变量的值插入到数据库中会话列的所有列中。和独特。
WHERE sessionCheck = $sessionCheck
WHERE sessionCheck = $sessionCheck
This works perfectly well for me on my Procedural PHP 7 (MySqli) and MySQL 5 这对我的过程PHP 7(MySqli)和MySQL 5来说非常合适
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.