我见过很多类似的问题,但它们都让我感到困惑,因为我没有使用$wpdb变量来获取数据。我使用简单的php方法通过调用wordpress数据库来获取数据。但不知怎么的,它只返回了一排。以下是用于获取数据的代码行。
$sql = "SELECT * FROM teacher_directory";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>S. No.</th>";
echo "<th>Name</th>";
echo "<th>Designation</th>";
echo "<th>Department</th>";
echo "<th>Tele/Mob.No.</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['S. No.'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Designation'] . "</td>";
echo "<td>" . $row['Department'] . "</td>";
echo "<td>" . $row['Tele/Mob.No.'] . "</td>";
echo "</tr>";
}
echo "</table>";
任何帮助或建议都是可观的。
使用此wordpress查询从数据库获取结果
global $wpdb;
$results = $wpdb->get_results( 'SELECT * FROM teacher_directory WHERE 1', ARRAY_A );
if(!empty($results)){
echo "<table>";
echo "<tr>";
echo "<th>S. No.</th>";
echo "<th>Name</th>";
echo "<th>Designation</th>";
echo "<th>Department</th>";
echo "<th>Tele/Mob.No.</th>";
echo "</tr>";
foreach($results as $row){
echo "<tr>";
echo "<td>" . $row['S. No.'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Designation'] . "</td>";
echo "<td>" . $row['Department'] . "</td>";
echo "<td>" . $row['Tele/Mob.No.'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
请使用$wpdb类(它具有直接访问和操作wordpress数据库的功能):
global $wpdb;
$sql = $wpdb->get_results( "SELECT * FROM teacher_directory" );
$results = $wpdb->get_results($sql) or die(mysql_error());
if($results) {
echo "<table>";
echo "<tr>";
echo "<th>S. No.</th>";
echo "<th>Name</th>";
echo "<th>Designation</th>";
echo "<th>Department</th>";
echo "<th>Tele/Mob.No.</th>";
echo "</tr>";
}
foreach( $results as $row ) {
echo "<tr>";
echo "<td>" . $row['S. No.'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Designation'] . "</td>";
echo "<td>" . $row['Department'] . "</td>";
echo "<td>" . $row['Tele/Mob.No.'] . "</td>";
echo "</tr>";
}
echo "</table>";