而只会检索最后一条记录

而只会检索最后一条记录

本文介绍了sql查询不会检索所有记录,而只会检索最后一条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

using PHP & MYSQL on WordPress and Google Map API in order to retrieve data from MYSQL database and display markers with info windows on Google Map.

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

problem is that the map doesn't appear on the webpage, yet the SQL query is retrieving the required data.

我有SQL查询的地方

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

that retrieve data and display it in table

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

example i have 3 records that must be displayed based on the user selectionbut the query just display the last record not the 3 records

查询中的错误在哪里:

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

?>

推荐答案

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

Move your table rows inside the while loop and your title line before the loop and then you will see all the data and not just the last line

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

?>

这篇关于sql查询不会检索所有记录,而只会检索最后一条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-10 22:05