Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我需要使用表中的雇员id从mysql数据库中获取数据_Php_Mysql - Fatal编程技术网

Php 我需要使用表中的雇员id从mysql数据库中获取数据

Php 我需要使用表中的雇员id从mysql数据库中获取数据,php,mysql,Php,Mysql,我需要使用员工id从表中获取数据 有两张桌子 1.“用户信息”,包含id、姓名、密码、电子邮件、日程日期等 2.“获取”包含id、ScheduleDate、starttime、endtime、hours和employeeid 如果我给出名称和密码,它将从“user_info”中获取特定用户的id,并将id作为员工id发送到表“fetch”,然后从该员工id获取数据 用户信息数据库 获取数据库 例如,如果我在user_info中以name:rajesh password:1995的形式输入,它

我需要使用员工id从表中获取数据

有两张桌子

1.“用户信息”,包含id、姓名、密码、电子邮件、日程日期等

2.“获取”包含id、ScheduleDate、starttime、endtime、hours和employeeid

如果我给出名称和密码,它将从“user_info”中获取特定用户的id,并将id作为员工id发送到表“fetch”,然后从该员工id获取数据

用户信息数据库

获取数据库

例如,如果我在user_info中以name:rajesh password:1995的形式输入,它应该获取该用户的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)); 

   ?>

我会使用“内部连接”来实现您想要的:

$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);

据我所知,在第二次查询之前,您的代码是正确的。在该查询执行中,您将获得多条记录。所以你需要使用循环来实现

请参见下面的示例代码:

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;

如果有帮助,请告诉我。

使用预先准备好的语句。关于在数据库中以明文形式存储密码的不可避免警告。这不是你问题的一部分,但你应该意识到危险。根据你的代码,你正在使用
surya
,它的
id
为9,但你想获取
employeeid
为15的数据。通常,您将永远无法获取属于15的数据,因为您使用的id为9。或者这只是一个例子?这是一个示例数据库,没有在任何地方使用,所以不用担心!@cmiller.示例数据库与否,您应该始终遵循最佳实践来养成编写好代码的习惯。它不起作用,先生,也没有显示任何错误,显示空白页。我已经编辑了您所说的代码,请检查我是否做对了!查看我的更新答案,我添加了while循环代码。在执行之前,请正确检查变量!谢谢你的帮助,一切正常,但仍在工作,所以我删除了数据库并再次创建了新的数据库,我运行了这段代码,工作正常。