简体   繁体   English

如何从数据库检索数据并将其显示在表中

[英]How to retrieve data from database and display it in table

I am using WordPress and the global class $wpdb in order to retrieve data from MySQL database and display the results in a table. 我正在使用WordPress和全局类$ wpdb以便从MySQL数据库检索数据并在表中显示结果。

I have 4 dropdown list that allow the user to select the required inputs then based on the selected inputs the system display all the related data for the user selection. 我有4个下拉列表,允许用户选择所需的输入,然后基于所选输入,系统显示所有相关数据供用户选择。

When I try to run the code it display error: 当我尝试运行代码时,显示错误:

Notice: Array to string conversion 注意:数组到字符串的转换

网页显示错误

first part of code : 代码的第一部分:

<?php
    /*
    Template Name: search info
    */

    get_header();
    ?>

    <?php
    // code for submit button ation
    global $wpdb,$_POST;
//variables that handle the retrieved data from mysql database
if(isset($_POST['site_name'])) 
      { 
       $site_name=$_POST['site_name'];
      }
      else { $site_name=""; }

if(isset($_POST['owner_name'])) 
     {
      $owner_name=$_POST['owner_name']; 
     } 
     else { $owner_name=""; }

if(isset($_POST['Company_name'])) 
     {
      $company_name=$_POST['Company_name'];
     } 
     else { $company_name=""; }

if(isset($_POST['Subcontractor_name'])) 
    { 
     $Subcontractor_name=$_POST['Subcontractor_name']; 
    }
    else { $Subcontractor_name="";}


$site_id = ['siteID'];
$equipment_type = ['equipmentTYPE'];
$lat=['latitude'];
$long=['longitude'];
$height = ['height'];
$owner_contact = ['ownerCONTACT'];
$sub_contact = ['subcontractorCONTACT'];
$sub_company = ['subcontractorCOMPANY'];


   if(isset($_POST['query_submit']))
   {
//query to retrieve all  related info of the selected data from the dropdown list  
$query_submit =$wpdb->get_results ("select 

site_info.siteID,site_info.siteNAME ,site_info.equipmentTYPE,site_coordinates.latitude,site_coordinates.longitude,site_coordinates.height ,owner_info.ownerNAME,owner_info.ownerCONTACT,company_info.companyNAME,subcontractor_info.subcontractorCOMPANY,subcontractor_info.subcontractorNAME,subcontractor_info.subcontractorCONTACT from `site_info`
LEFT JOIN `owner_info`
on site_info.ownerID = owner_info.ownerID
LEFT JOIN `company_info` 
on site_info.companyID = company_info.companyID
LEFT JOIN `subcontractor_info` 
on site_info.subcontractorID = subcontractor_info.subcontractorID
LEFT JOIN `site_coordinates` 
on site_info.siteID=site_coordinates.siteID 

where 
site_info.siteNAME = `$site_name` 
AND
owner_info.ownerNAME = `$owner_name`
AND
company_info.companyNAME = `$company_name`
AND
subcontractor_info.subcontractorNAME = `$Subcontractor_name`
 ");
 ?>
    <table width="30%" >
        <tr>
           <td>Site Name</td>
           <td>Owner Name</td>
           <td>Company Name</td>
           <td>Subcontractor Name</td>
         </tr>
         <tr>
            <td><?php echo $site_name ; ?></td>
            <td><?php echo $owner_name ; ?></td>
            <td><?php echo $company_name ; ?></td>
            <td><?php echo $Subcontractor_name ; ?></td>
            <td><?php echo $site_id ; ?></td>
            <td><?php echo $equipment_type ; ?></td>
            <td><?php echo $lat ; ?></td>
            <td><?php echo $long ; ?></td>
            <td><?php echo $height ; ?></td>
            <td><?php echo $owner_contact ; ?></td>
            <td><?php echo $sub_contact ; ?></td>
            <td><?php echo $sub_company ; ?></td>


         </tr>
    </table>
    <?php }  ?>

The second part of code is for retrieve data from database and includes it in the dropdown list. 代码的第二部分用于从数据库检索数据,并将其包括在下拉列表中。

I will appreciate any help. 我将不胜感激。

You can get rid of the "Array to string conversion" error quite easy. 您可以轻松摆脱“数组到字符串转换”错误。

In these lines, you are creating arrays: 在这些行中,您正在创建数组:

$site_id = ['siteID'];
$equipment_type = ['equipmentTYPE'];
$lat=['latitude'];
...
$sub_company = ['subcontractorCOMPANY'];

...which you later are trying to echo. ...您稍后会尝试回应。 You simply can't echo arrays. 您根本无法回显数组。

Just change the above to be strings instead: 只需将上面的内容更改为字符串即可:

$site_id = 'siteID';
$equipment_type = 'equipmentTYPE';
$lat = 'latitude';
...
$sub_company = 'subcontractorCOMPANY';

Note: As others already pointed out, your code is wide open to SQL Injections. 注意:正如其他人已经指出的那样,您的代码对SQL注入很开放。 You should really escape your data, before using it in any queries. 在任何查询中使用数据之前,您实际上应该转义数据。

<table border="1">
<tr>
 <th>Firstname</th>
 <th>Lastname</th>
 <th>Points</th>
</tr>
  <?php
    global $wpdb;
    $result = $wpdb->get_results ( "SELECT * FROM myTable" );
    foreach ( $result as $print )   {
    ?>
    <tr>
    <td><?php echo $print->firstname;?></td>
    </tr>
        <?php }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM