使用select查询不起作用,以便从数据库中检索所需的数据

时间:2017-02-27 11:23:03

标签: php mysql wordpress foreach

我在Wordpress中使用PHP和MYSQL,以便根据用户选择从数据库中检索数据,但此查询不会返回任何数据。

我正在使用 $ wpdb 类与 prepare() get_results()

并尝试回显查询以调试代码,并分配正确的变量,以便对数据库中的值和用户的输入进行比较。 / p>

sql查询:

    $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_coordinates c
        on i.siteID=c.siteID 
        where 
        i.siteNAME = %s
        AND 
        o.ownerNAME = %s
        AND 
        x.companyNAME = %s
       ",$site_name,$owner_name,$company_name);
    echo $site_name;
     $query_submit =$wpdb->get_results($sql);

foreach ($query_submit as $obj) {
             echo "query is working";
    echo "<table width='30%' ";
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的代码

echo $ sql; 它显示了sql查询,其值已分配给正确的变量。

任何人都可以告诉我我的错误在哪里使查询停止不运行到foreach循环? trying to debug

supposed returned data

在我更改查询并将值作为字符串代替%s 后,我得到了上述结果。 我意识到它的问题是什么,因为%s 是一个数组,但是当我放一个字符串时它工作正常。  我正在将字符串转换为数组,因为我没有转换,我将收到此错误:

Xdebug: Fatal error: Uncaught Error: Cannot use object of type stdClass as array in /opt/lampp/htdocs/wordpress/wp-content/themes/wp-portfolio/search-info.php:245 Stack trace: #0 /opt/lampp/htdocs/wordpress/wp-includes/template-loader.php(74): include() #1 /opt/lampp/htdocs/wordpress/wp-blog-header.php(19): require_once('/opt/lampp/htdo...') #2 /opt/lampp/htdocs/wordpress/index.php(17): require('/opt/lampp/htdo...') #3 {main} thrown in /opt/lampp/htdocs/wordpress/wp-content/themes/wp-portfolio/search-info.php on line 245. Output triggered in /opt/lampp/htdocs/wordpress/wp-content/plugins/query-monitor/collectors/php_errors.php on line 163

1 个答案:

答案 0 :(得分:0)

我解决了我的问题,我在查询中迭代以将所选数据检索为数组。 这是我正在使用的代码:

             <?php


                 $query_site_name =$wpdb->get_results ("select DISTINCT siteNAME  from site_info");
                  foreach($query_site_name as $site_name)
                  {
                   $site_name = (array)$site_name;
                   echo "<option value = '{".$site_name ['siteNAME']."}'>".  $site_name['siteNAME']."</option>";
                  } 
             ?>

我将迭代过程从数组更改为对象,因此它变为:

 <?php


                     $query_site_name =$wpdb->get_results("select DISTINCT siteNAME  from site_info");
                      foreach($query_site_name as $site_name)
                      {
            //           $site_name = (array)$site_name;
                       echo "<option value = '".$site_name ->siteNAME."'>".  $site_name->siteNAME."</option>";
                      } 
                 ?>