在wordpress插件开发中,我想使用where查询从数据库中获取数据

时间:2018-10-30 05:58:46

标签: wordpress

我正在尝试从mysql数据库检索数据。这是我的wordpress插件代码。这是代码不起作用

<form method="post">
<input type="text" name="fname" placeholder="fullname">
<button type="submit">Submit</button>
</form>
<?php 
 global $wpdb;
    $table_name=$wpdb->prefix.'mytable';
    if(isset($_POST['submit'])) 
     { 
      $name=$_POST['fname'];

      $query="select * from $table_name where `fname`= '$name'";
      $query_result=$wpdb->get_results($query);


      foreach( $query_result as $DBP_row)
    {
         <tr>
       <td><?php echo $id; ?></td>
      <td><?php echo $name; ?></td>
      <td><?php  echo $email; ?></td>
      </tr>
    }
     }

1 个答案:

答案 0 :(得分:0)

您可以尝试这种格式

<form method="post">
 <input type="text" name="fname" placeholder="fullname">
<button type="submit">Submit</button>
</form>
<?php 
 global $wpdb;
    $table_name=$wpdb->prefix.'mytable';
    if(isset($_POST['submit'])) 
     { 
      $name=$_POST['fname'];

      $query="select * from $table_name where `fname`= '$name'";
      $query_result=$wpdb->get_results($query);


      foreach( $query_result as $DBP_row)
    {
         <tr>
       <td><?php echo $DBP_row->id; ?></td>
      <td><?php echo $DBP_row->name; ?></td>
      <td><?php  echo $DBP_row->email; ?></td>
      </tr>
    }
     }

或以这种方式获取数据

<form method="post">
     <input type="text" name="fname" placeholder="fullname">
    <button type="submit">Submit</button>
    </form>
    <?php 
     global $wpdb;
        $table_name=$wpdb->prefix.'mytable';
        if(isset($_POST['submit'])) 
         { 
          $name=$_POST['fname'];

          $query="select * from $table_name where `fname`= '$name'";
          $query_result=$wpdb->get_results($query,ARRAY_A);


          foreach($query_result as $DBP_row)
        {
             <tr>
           <td><?php echo $DBP_row['id']; ?></td>
          <td><?php echo $DBP_row['name']; ?></td>
          <td><?php  echo $DBP_row['email']; ?></td>
          </tr>
        }
         }