我需要使用员工ID从表中获取数据,
有两张桌子
1.“ user_info”,其中包含ID,名称,密码,电子邮件,ScheduleDate等!
2.“获取”,其中包含id,ScheduleDate,开始时间,结束时间,小时数,员工编号。
如果我提供名称和密码,它将从“ user_info”获取特定用户的ID,并且应将ID作为员工ID发送到表“ fetch”,并且应该从该员工ID获取数据。
User_info数据库
获取数据库
例如,如果我在user_info中输入名称为:rajesh password:1995,则它应使用该用户的ID,并且应将ID作为员工ID作为15发送到“提取”表。
当我尝试将ID发送为员工ID时,它不会打印任何内容,也没有显示任何错误。
<?php
require "init.php";
$name = "surya";
$password = "1995";
$Sql = "SELECT * FROM `user_info`
WHERE `name`='".$name."' AND
`password`='".$password."';";
$result = mysqli_query($con, $Sql);
$retrive = array();
while($row = mysqli_fetch_array($result))
{
$user_id = $row['id'];
$sql = "SELECT id, ScheduleDate, StartTime,Endtime, Hours,Employeeid
FROM empdet WHERE Employeeid ='".$user_id."' ";
$result = $con->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
$id=$row["id"].
$date=$row["ScheduleDate"];
$start=$row["StartTime"];
$end=$row["Endtime"];
$hour=$row["Hours"];
$Employeeid=$row["Employeeid"];
list($year,$month,$day) = split("-",$date);
$data[] = array("year"=>$year,
"month"=>$month,
"day"=>$day,
"StartTime"=>$start,
"Endtime"=>$end,
"Hours"=>$hour );
}
$response = $data;
} else
{
echo "0 results";
}
}
echo json_encode(array("user_data"=> $response));
?>
我需要使用员工ID 15从“提取”表中提取所有3个数据。有人可以帮忙找到它吗?
据我了解,您的代码在第二次查询之前是正确的。在该查询执行中,您将获得多个记录。因此,您需要为此使用循环。
请参阅以下示例代码:
while ($rows = $result->fetch_assoc()) {
$date=$rows["ScheduleDate"];
$start=$rows["StartTime"];
$end=$rows["Endtime"];
$hour=$rows["Hours"];
list($year,$month,$day) = split("-",$date);
$data[] = array("year"=>$year,
"month"=>$month,
"day"=>$day,
"StartTime"=>$start,
"Endtime"=>$end,
"Hours"=>$hour );
}
$response = $data;
让我知道是否有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句