我需要使用表

时间:2017-12-28 04:34:08

标签: php mysql

我需要使用员工ID

从表中获取数据

有两个表

1" USER_INFO"其中包含id,name,password,email,ScheduleDate等!

2"取"其中包含id,ScheduleDate,starttime,endtime,hours,employeeid。

如果我提供名称和密码,则需要特定用户的ID来自" user_info"并且它应该将id作为雇员id发送到表" fetch"它应该从该员工ID中获取数据。

User_info数据库

enter image description here

获取数据库

enter image description here

例如,如果我将输入作为名称:rajesh密码:1995在user_info中,它应该采用该用户的id,并且它应该将id作为员工ID 15发送到" fetch"表

当我尝试将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)); 

   ?>

我需要从&#34; fetch&#34;中获取所有3个数据使用员工ID 15的表。任何人都可以帮忙找到它吗?

2 个答案:

答案 0 :(得分:1)

从我的理解,你的代码是正确的,直到第二次查询。在该查询执行中,您将获得多个记录。所以你需要使用循环。

见下面的示例代码:

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;

如果有帮助,请告诉我。

答案 1 :(得分:0)

我会使用'INNER JOIN'来实现你想要的目标:

$sql = "SELECT fetch.ScheduleDate, fetch.StartTime, fetch.Endtime, fetch.Hours"
." FROM fetch INNER JOIN user_info ON fetch.id = user_info.id"
." WHERE user_info.name = ? AND user_info.password = ? ";

//Prepare statement
$stmt = $mysqli_prepare($con, $sql);

//Bind parameter (id)
mysqli_stmt_bind_param($stmt, ss, $name, $password);

//Execute statement
mysqli_stmt_execute($stmt);

//Bind results
mysqli_stmt_bind_result($stmt, $date, $start, $end, $hour);

//Fetch values (puts the values into the variables)
mysqli_stmt_fetch($stmt);