0

How to retrieve data from MYSQL database using php on Wordpress ?

I have a table that includes 4 dropdown lists each one of them includes data retrieved from the database, I want the user to choose from the 4 droplists and based on the user's selection I will display in a second table all the related fields of the request.

So I write a query that retrieve data from 5 tables.

But the system display a warning and do not display anything.

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, array..

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`
     ");

         If(mysqli_num_rows($query_submit)>0)
           {
             while($row=mysql_fetch_array($query_submit))
             {  
            ?>

        <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 $row['site_name']; ?></td> 
                <td><?php echo $row['owner_name']; ?></td> 
                <td><?php echo $row['company_name']; ?></td> 
                <td><?php echo $row['Subcontractor_name']; ?></td> 
                <td><?php echo $row['site_id']; ?></td> 
                <td><?php echo $row['equipment_type']; ?></td>
                <td><?php echo $row['lat']; ?></td> 
                <td><?php echo $row['long']; ?></td> 
                <td><?php echo $row['height']; ?></td> 
                <td><?php echo $row['owner_contact']; ?></td> 
                <td><?php echo $row['sub_contact']; ?></td> 
                <td><?php echo $row['sub_company']; ?></td>

             </tr>
        </table>

        <?php 
          } 
       } 

    }  
    ?>
<?php
get_footer();
?>

I will appreciate any help.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nabil Jaroush
  • 49
  • 3
  • 12
  • `mysqli_num_rows` needs your connection as first parameter. Note that you are switching between `mysqli_*` and `mysql_*` in your code. Stick to `mysqli_*`. – MrDarkLynx Feb 17 '17 at 12:11
  • @ Shadow its not a duplication for this question if you can help me i appreciate that – Nabil Jaroush Feb 17 '17 at 12:18
  • The TL;DR reason is you can't mix and match the WPDB object with mysqli. WPDB has its own methodology for fetching and looping through results – Machavity Feb 17 '17 at 13:21

0 Answers0