我的数据库由不同的表组成,我关心的表是user
和module
,在用户登录后,第二个查询必须从模块用户返回max(id)
。我想要做的是:我想获得登录的同一用户的max(id)
,用户存储在create_user
列的模块表中,是否可以这样做以及应该如何条件是哪里?
表格结构:
用户表:(id int pri.key, username varchar, password varchar)
。
模块表:(id int pri.key, name varchar, creat_time date, create_user int)
插入新模块时,用户ID存储在create_user
。
代码:
<?php
$json["status"] = "running";
$details[] = "started get_tables ";
include_once('confi.php');
$request_body = file_get_contents('php://input');
$statement = "INSERT INTO tbl_archive (content) VALUES ('$request_body' );";
mysql_query($statement);
$input = json_decode($request_body, true);
$user = $input["user"];
$password = $user["password"];
$mySQLstring = "SELECT username, password, id FROM tbl_user where username = '$username' ;";
$json["statement"][] = $mySQLstring;
$qur = mysql_query($mySQLstring);
if ($qur){
$max = mysql_fetch_assoc($qur);
}
if ($max){
$json["max"] = $max;
if ($max["password"] == $password){
$json["username"] = $username;
$json["id"] = $max["id"];
$json["status"] = "ok";
$tables = array("class", "class_user", "module", "module_class", "module_user", "rating", "student", "student_class");
foreach($tables as $table){
$id= $max["id"];
if ( $table == 'module' ){
$statement ='SELECT create_user, MAX(id) AS id FROM tbl_'.$table;
$statement .= ' WHERE create_user =$id' ;
$qur = mysql_query($statement);
if ($qur){
while($r = mysql_fetch_array($qur, MYSQL_ASSOC)){
$result[$table][] = $r;
}
}
}
}
$json = array("status" => "ok", "data" => $result);
}
}
}
@mysql_close($conn);
header('Content-type: application/json');
echo json_encode($json);
?>
答案 0 :(得分:0)
因此tbl_user.id
和tbl_module.create_user
是用户ID。您查询了tb_user并将结果提取到$max
。
$statement = 'SELECT create_user, MAX(id) AS id FROM tbl_'.$table;
$statement .= ' WHERE create_user = ' . $max['id'];