我有兩個表,我想獲取兩個表值,這個代碼應該工作正常,但count = 2表示循環運行2次,返回2次的輸出,如count = 3循環3次運行返回輸出3次,我做了什麼錯....whilelopp獲取兩個表值時不正確
輸出
{
"status":"success",
"count":2,
"data":[
{
"id":"1",
"t_id":"STV1",
"t_title":"Horoscope Uploading",
"t_project":"1",
"t_sub_project":"Sub-project",
"t_desc":"cfdgdgdcf",
"t_priority":"Urgent",
"t_assign_to":"AE098",
"t_assign_on":"2016-04-13 12:03:49",
"t_started_on":"2016-04-14 05:30 PM",
"t_due_on":"2016-04-22 05:30 PM",
"t_complete_percentage":"100",
"t_est_hours":"35",
"t_worked":"10 Hours",
"t_comment":"dfhfghfgfsfhfgh",
"t_created_on":"2016-04-13 12:03:49",
"t_edited_on":"2016-04-14 07:01:06",
"t_status":"3",
"t_delete_on":"0"
}
]
}{
"status":"success",
"count":2,
"data":[
{
"id":"1",
"t_id":"STV1",
"t_title":"Horoscope Uploading",
"t_project":"1",
"t_sub_project":"Sub-project",
"t_desc":"cfdgdgdcf",
"t_priority":"Urgent",
"t_assign_to":"AE098",
"t_assign_on":"2016-04-13 12:03:49",
"t_started_on":"2016-04-14 05:30 PM",
"t_due_on":"2016-04-22 05:30 PM",
"t_complete_percentage":"100",
"t_est_hours":"35",
"t_worked":"10 Hours",
"t_comment":"dfhfghfgfsfhfgh",
"t_created_on":"2016-04-13 12:03:49",
"t_edited_on":"2016-04-14 07:01:06",
"t_status":"3",
"t_delete_on":"0"
}
]
}
同答,但我會來兩次,如何解決這個問題
<?php
\t session_start();
\t include('dbconfig.php');
\t if(empty($_SESSION['email'])){
\t header('Location:login.php');
\t }
\t $dapartment = $_POST['department'];
\t $sql = mysql_query("SELECT * FROM task_employee WHERE emp_designation='$dapartment'");
\t $count=mysql_num_rows($sql);
\t $return = array();
\t while($r=mysql_fetch_assoc($sql)){
\t \t $emp_id=$r['emp_id'];
\t \t if($count > 0){
\t \t $mysql = mysql_query("SELECT * FROM task WHERE t_assign_to='$emp_id'");
\t \t while($row= mysql_fetch_assoc($mysql)){
\t \t \t $data[] = $row;
\t \t \t }
\t \t \t $return=array('status'=>'success','count'=>$count,'data'=>$data);
\t \t \t echo json_encode($return);
\t \t }else{
\t \t \t $return=array('status'=>'not found','count'=>$count,'data'=>$data);
\t \t \t echo json_encode($return);
\t \t }
\t } \t
?>
你到底面臨什麼問題? –
爲什麼在這裏不使用連接的查詢而不是嵌套循環? – Wobbles
@TirthPatel你錯了!有php5-mysqlnd擴展來支持舊代碼。必須同意這種類型的代碼在大多數應用程序中都超過了PDO。 – num8er