首页 > 解决方案 > 在 wordpress 插件开发中,我想使用 where 查询从数据库中获取数据

问题描述

我正在尝试从 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>
    }
     }

标签: 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 $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>
        }
         }

推荐阅读