I have seen many similar questions But they all are confusing for me because I am not using $wpdb variable to fetch data. I am using simple php way to fetch data By calling wordpress database. But somehow its returning only one row. here is the lines of code which are being used to fetch data.
$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>";
Any help or suggestion will be appreciable.
Use this wordpress query fetch results from database
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>";
}
Please use the $wpdb class (which has functions for directly accessing and manipulating the wordpress database):
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>";