Php 在另一个查询的WHERE条件下使用查询中的数据
我的数据库由不同的表组成,我关心的表是,Php 在另一个查询的WHERE条件下使用查询中的数据,php,mysql,Php,Mysql,我的数据库由不同的表组成,我关心的表是,user和module,在用户登录后,第二个查询必须从模块用户返回max(id)。我想做的是:我想获得登录的同一用户的max(id),该用户存储在create\u user列的模块表中,可以这样做吗?where条件应该是什么 表结构: 用户表:(id int pri.key,username varchar,password varchar) 模块表:(id int pri.key,name varchar,create\u time date,creat
user
和module
,在用户登录后,第二个查询必须从模块用户返回max(id)
。我想做的是:我想获得登录的同一用户的max(id)
,该用户存储在create\u user
列的模块表中,可以这样做吗?where条件应该是什么
表结构:用户表:
(id int pri.key,username varchar,password varchar)
模块表:
(id int pri.key,name varchar,create\u time date,create\u user int)
插入新模块时,用户id存储在create\u 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);
?>
Sotbl\u user.id
和tbl\u module.create\u user
是用户id。您查询了tb_用户并将结果提取到$max
中
$statement = 'SELECT create_user, MAX(id) AS id FROM tbl_'.$table;
$statement .= ' WHERE create_user = ' . $max['id'];
我不太明白问题出在哪里。为什么不将用户名从第一个查询结果连接到您的where
?!顺便说一句,mysql\u query
等都是不推荐的和不安全的,最好使用mysqli或PDO。首先,因为用户名没有插入模块表中,而是用户id插入到create\u user列中,其次,人如何将用户名甚至id连接到where。第三,谢谢你关于MySQL的说明,我注意到它不再被使用了,取而代之的是MySQL,也许这是你应该在问题中加入一些表结构的地方。我添加了表结构,我尝试了代码中的where,但它不起作用使用单引号内的$id
,将不起作用。用双引号就可以了。