wordpress - 在 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>
}
}
解决方案
你可以试试这个格式
<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>
}
}
推荐阅读
- xcode - RCTConvert.h: 'yoga/Yoga.h' 文件未找到
- laravel - Laravel 中间件认证
- json - 在greenplum pxf外部表中,从对象的json数组中获取元素时获取空字符串
- node.js - bash 没有运行 npm 或 npx 命令
- php - 使用 PHP 从间接链接下载 XLSX 文件
- python - Streamlit 面临的问题
- redis - redis中key的重置值
- python - 从 numpy 数组中传递要从开始顺序扣除的数字
- java - 用 Arraylist 替换 Vector(insertElementAt 问题)
- java - 带有弹性搜索的 Spring Boot 导致 java.lang.NoSuchFieldError: IGNORE_DEPRECATIONS