简体   繁体   English

如何在PHP和MySQL中为多个用户动态地从数据库中获取数据

[英]how to fetch data from database dynamically for multiple user in PHP and MySQL

This question is bit complex atleast for me. 这个问题对我来说至少有点复杂。 Well, I am working on a project and need some bit more help.. 好吧,我正在做一个项目,需要更多帮助。

Actually i have a module , where when 1 user login he get limited data from 1 table and he update its records. 实际上,我有一个模块,其中1个用户登录时,他从1个表中获取有限的数据并更新其记录。 That is done ! 做完了!

second thing, the issue : i have 5 user who will login and will work on data. 第二件事,问题:我有5位用户将登录并处理数据。

but no user should get same data, like we have 1000 records. 但没有用户应该获得相同的数据,例如我们有1000条记录。

Then when 1st user login: he get 10 records from 1 - 10. 2nd user login : he get next 10 ; 然后,当第一个用户登录时:他从1-10中获得10条记录。 11- 20. same so on.. 11- 20.依此类推..

and when next day they login ; 当第二天他们登录; they get records from 51. because 5 user last day worked on first 50 data records. 他们从51获得记录。因为最后一天有5位用户处理了前50条数据记录。

My issue is how to achieve that 2 goals ? 我的问题是如何实现这两个目标?

do i need a framework for this ? 我需要一个框架吗?

or it can be done using simple php n sql ? 还是可以使用简单的php n sql完成?

any support will be helpful for me. 任何支持都会对我有所帮助。 :) :)

Ok. 好。 This is just a raw answer to give you a better idea. 这只是给您一个更好的主意的原始答案。 This is how you will insert the login . 这就是您插入登录名的方式。

Consider having a table containing following fields, 考虑使用包含以下字段的表格,

Table Name: Temp_Table 表名称:Temp_Table

user, assigned_rows_last_no, date_assigned 用户,assigned_rows_last_no,date_assigned

<?php
$con=mysqli_connect("example.com","hiren","abc123","my_db");

// These things can be included into a single file say config.php and including in every file so that you dont need to specify connection string everytime.

if (mysqli_connect_errno())
 {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
     } 

//This query required to be included into the file, which is exactly after login.php

$sql = mysqli_query($con,"SELECT assigned_rows_last_no FROM Temp_Table ORDER BY assigned_rows_last_no DESC LIMIT 1");
// This is because I want the last row number assigned to the user.

IF ($sql == 0) // Check whether the return answer is NULL or not.
{
    // If the result is empty than add new entry of the user with defined row number.
    // Suppose that current username can be retrieved from SESSION and stored into a variable.

    $insertquery = mysqli_query($con, "INSERT INTO Temp_Table Values ('" . $username . $"', 10, CURDATE()");
    mysqli_close($con);
}
else
{
    // Select the last entry of row and add 10 to it. Ex. User2 has been assigned to 11-20, table contains 20 in the row of user2, now user3 comes, this query will select the row_no, add 10 and insert again into the table (i.e. value 30 which means 21-30.)

    settype($sql, "int");
    $sql = $sql + 10;
    $insertquery = mysqli_query($con, "INSERT INTO Temp_Table Values ('" . $username . $"', '" . $sql . "', CURDATE()");
    mysqli_close($con);
}

mysqli_close($con);
?>

The field Date will help you to recognize the entries of today, so that you can set your logic for "There should be no duplicate entries for the same user on same day" 日期字段将帮助您识别今天的条目,因此您可以将逻辑设置为“同一天同一用户不应有重复的条目”

Now, Make you own page, which check the above mentioned things, and assign the rows to the users. 现在,创建您自己的页面,该页面检查上述内容,并将行分配给用户。

Note: This code will only be able to clear out the logic for you, I am not sure whether it will work in your code without any changes. 注意:该代码只能为您清除逻辑,我不确定它是否可以在您的代码中正常工作而无需进行任何更改。

You don't need a extra framework. 您不需要额外的框架。 Simply done with php 'n' sql! 只需用php'n'sql完成! Why you don't save the last edited lines (linenumbers) in a extra SQL-Table? 为什么不将最后编辑的行(行号)保存在额外的SQL表中? Maybe with the username / userid. 也许使用用户名/用户名。

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

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