简体   繁体   English

如何从两个基于会话变量php的表中获取数据?

[英]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并按以下步骤使用程序步骤:

  • Step 1: Just Start Session in all your pages- session_start(); 第1步:只需在您所有的页面中启动会话-session_start();
  • Step 2: In your Db create a column for Session eg sessionCheck 步骤2:在Db中为Session创建一列,例如sessionCheck
  • Step 3: On the Sign-up page start session and use MySqli last insert function like: 步骤3:在“注册”页面上启动会话,并使用MySqli最后插入函数,例如:

$_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"];在执行插入操作时,将此变量的值插入到数据库中会话列的所有列中。和独特。

  • Step 4: On the Display Page use control structure specifically Nested IF instead of INNER JOIN. 步骤4:在显示页面上,使用专门用于嵌套IF的控件结构代替INNER JOIN。
  • STEP 5: ALL your Insertion insert the value of $_sessionCheck into sessionCheck in all your tables except the sign-up page (because the session id is originally picked from sign-up page so you don't need to insert into sign up again) 步骤5:您的所有插入操作都会将$ _sessionCheck的值插入除注册页面之外的所有表的sessionCheck中(因为会话ID最初是从注册页面中选取的,因此您无需再次插入注册)
  • Step6: All your Select should be tie to WHERE sessionCheck = $sessionCheck 步骤6:您的所有Select应该绑定到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.

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