sql查询不检索所有记录只检索最后一条记录

时间:2017-03-13 14:24:23

标签: php mysql left-join

使用PHP& WordPress和Google Map API上的MYSQL,用于从MYSQL数据库中检索数据,并在Google Map上显示带有信息窗口的标记。

问题是地图没有出现在网页上,但SQL查询正在检索所需的数据。

我有SQL查询:

检索数据并将其显示在表格中

示例我有3条记录必须根据用户选择显示 但查询只显示最后一条记录而不是3条记录

查询中出现错误:

查询:

$sql = $wpdb->prepare("select i.siteID
     , i.siteNAME
     , i.equipmentTYPE
     , c.latitude
     , c.longitude
     , c.height 
     , o.ownerNAME
     , o.ownerCONTACT
     , x.companyNAME
     , y.subcontractorCOMPANY
     , y.subcontractorNAME
     , y.subcontractorCONTACT
  from site_info i
  LEFT  
  JOIN owner_info o
    on i.ownerID = o.ownerID
  LEFT  
  JOIN company_info x
    on i.companyID = x.companyID
  LEFT 
  JOIN subcontractor_info y
    on i.subcontractorID = y.subcontractorID
    LEFT JOIN site_coordinates2 c
    on i.siteID=c.siteID 
    where 
    i.siteNAME = %s
    AND 
    o.ownerNAME = %s
    AND 
    x.companyNAME = %s
   ",$site_name,$owner_name,$company_name);

 $query_submit =$wpdb->get_results($sql, OBJECT);

    echo "<br>";
    echo "<br>";
//echo $sql;

//    var_dump($_POST['site_name']);

foreach ($query_submit as $obj) {



$obj->siteNAME;
$obj->ownerNAME;
$obj->companyNAME;
$obj->subcontractorNAME;
$obj->siteID;
$obj->equipmentTYPE;
$obj->latitude;
$obj->longitude;
$obj->height;
$obj->ownerCONTACT;
$obj->subcontractorCONTACT;
$obj->subcontractorCOMPANY;

    } 

// table that will dsiplay the results based on the user's selection //
   echo "<table width='30%' ";

echo     "<tr>";
echo           "<td>Site Name</td>";
echo           "<td>Owner Name</td>";
echo           "<td>Company Name</td>";
echo           "<td>Subcontractor Name</td>";
echo           "<td>Site ID</td>";
echo           "<td>Equipment Type</td>";
echo           "<td> Lattitude</td>";
echo           "<td>Longitude </td>";
echo           "<td> Height</td>";
echo           "<td> Owner Contact</td>";
echo           "<td> Sub Contact</td>";
echo           "<td> Sub company Name</td>";
echo   "</tr>";  
echo   "<tr>";        
echo         "<td>".$obj->siteNAME."</td>";
echo         "<td>".$obj->ownerNAME."</td>";
echo         "<td>".$obj->companyNAME."</td>";
echo         "<td>".$obj->subcontractorNAME."</td>";
echo         "<td>".$obj->siteID."</td>";
echo         "<td>".$obj->equipmentTYPE."</td>";
echo         "<td>".$obj->latitude."</td>";
echo         "<td>".$obj->longitude."</td>";
echo         "<td>".$obj->height."</td>";
echo         "<td>".$obj->ownerCONTACT."</td>";
echo         "<td>".$obj->subcontractorCONTACT."</td>";
echo         "<td>".$obj->subcontractorCOMPANY."</td>";
echo  "</tr>";

echo  "<tr>";
echo     "<td>";

?>

2 个答案:

答案 0 :(得分:4)

在while循环中移动表行,在循环之前移动标题行,然后您将看到所有数据,而不仅仅是最后一行

$sql = $wpdb->prepare("select i.siteID
     , i.siteNAME, i.equipmentTYPE, c.latitude
     , c.longitude, c.height , o.ownerNAME
     , o.ownerCONTACT, x.companyNAME, y.subcontractorCOMPANY
     , y.subcontractorNAME, y.subcontractorCONTACT
  from site_info i
    LEFT JOIN owner_info o on i.ownerID = o.ownerID
    LEFT  JOIN company_info x on i.companyID = x.companyID
    LEFT JOIN subcontractor_info y on i.subcontractorID = y.subcontractorID
    LEFT JOIN site_coordinates2 c on i.siteID=c.siteID 
  where i.siteNAME = %s
    AND o.ownerNAME = %s
    AND x.companyNAME = %s
   ",$site_name,$owner_name,$company_name);

$query_submit =$wpdb->get_results($sql, OBJECT);

echo "<br>";
echo "<br>";

// table that will dsiplay the results based on the user's selection //
echo "<table width='30%' ";
echo     "<tr>";
echo           "<td>Site Name</td>";
echo           "<td>Owner Name</td>";
echo           "<td>Company Name</td>";
echo           "<td>Subcontractor Name</td>";
echo           "<td>Site ID</td>";
echo           "<td>Equipment Type</td>";
echo           "<td> Lattitude</td>";
echo           "<td>Longitude </td>";
echo           "<td> Height</td>";
echo           "<td> Owner Contact</td>";
echo           "<td> Sub Contact</td>";
echo           "<td> Sub company Name</td>";
echo   "</tr>";  

foreach ($query_submit as $obj) {
    echo   "<tr>";        
    echo         "<td>".$obj->siteNAME."</td>";
    echo         "<td>".$obj->ownerNAME."</td>";
    echo         "<td>".$obj->companyNAME."</td>";
    echo         "<td>".$obj->subcontractorNAME."</td>";
    echo         "<td>".$obj->siteID."</td>";
    echo         "<td>".$obj->equipmentTYPE."</td>";
    echo         "<td>".$obj->latitude."</td>";
    echo         "<td>".$obj->longitude."</td>";
    echo         "<td>".$obj->height."</td>";
    echo         "<td>".$obj->ownerCONTACT."</td>";
    echo         "<td>".$obj->subcontractorCONTACT."</td>";
    echo         "<td>".$obj->subcontractorCOMPANY."</td>";
    echo  "</tr>";

} 

echo '</table>';   

?>

答案 1 :(得分:0)

如果仔细查看代码,您会看到foreach循环继续为$obj分配不同的属性值。 但你并没有echo他们出去。所以什么都不会出现。 然后当循环结束时,$obj具有表格最后一行的属性值。然后你回应他们。

您需要做的是echo foreach循环内的表格行。{1}}因此,只要将值分配给$obj,您就echo了。

循环后,关闭表格。

echo "<table width='30%'>";

echo     "<tr>";
echo           "<td>Site Name</td>";
echo           "<td>Owner Name</td>";
echo           "<td>Company Name</td>";
echo           "<td>Subcontractor Name</td>";
echo           "<td>Site ID</td>";
echo           "<td>Equipment Type</td>";
echo           "<td> Lattitude</td>";
echo           "<td>Longitude </td>";
echo           "<td> Height</td>";
echo           "<td> Owner Contact</td>";
echo           "<td> Sub Contact</td>";
echo           "<td> Sub company Name</td>";
echo   "</tr>";

foreach ($query_submit as $obj) {

$obj->siteNAME;
$obj->ownerNAME;
$obj->companyNAME;
$obj->subcontractorNAME;
$obj->siteID;
$obj->equipmentTYPE;
$obj->latitude;
$obj->longitude;
$obj->height;
$obj->ownerCONTACT;
$obj->subcontractorCONTACT;
$obj->subcontractorCOMPANY;

echo   "<tr>";        
echo         "<td>".$obj->siteNAME."</td>";
echo         "<td>".$obj->ownerNAME."</td>";
echo         "<td>".$obj->companyNAME."</td>";
echo         "<td>".$obj->subcontractorNAME."</td>";
echo         "<td>".$obj->siteID."</td>";
echo         "<td>".$obj->equipmentTYPE."</td>";
echo         "<td>".$obj->latitude."</td>";
echo         "<td>".$obj->longitude."</td>";
echo         "<td>".$obj->height."</td>";
echo         "<td>".$obj->ownerCONTACT."</td>";
echo         "<td>".$obj->subcontractorCONTACT."</td>";
echo         "<td>".$obj->subcontractorCOMPANY."</td>";
echo  "</tr>";

}

echo "</table>";