Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having trouble trying to extract the data from mysql, I have tried every tutorial but still have problems. It only seems to show and loop the title and not the data. Not sure where the problem lies

<html>
  <head></head>
  <body>
    <?php
      $con = mysqli_connect("localhost","root","","oatdistribution");
      $tabsql = "SELECT 'Name', 'LivingCondition', 'OatType', 'NoOfOat', 'Status' FROM OatDis"; 
      $data = mysqli_query($con,$tabsql);
      $total = mysqli_num_rows($data);
      if($total > 0){
        while($row = mysqli_fetch_assoc($data)){
          echo $row['Name']."<br>";
        }
      }
    ?>
  </body>
</html>


What I have tried:

I have tried multiple types of tutorial online and yet still the same
Posted
Updated 23-Mar-21 4:58am
Comments
Richard MacCutchan 23-Mar-21 9:48am    
How many rows did the original query return?
Allysha April 23-Mar-21 10:53am    
it has extactly the same amount of rows that my data has which is 12

1 solution

Quote:
SQL
SELECT 'Name', 'LivingCondition', 'OatType', 'NoOfOat', 'Status' FROM OatDis
Your query isn't selecting any data from the table. Instead, it's returning a set of literal strings for each row in your table.

I suspect you're trying to quote the column names. To do that, you need to use back-ticks, not single quotes:
SQL
SELECT `Name`, `LivingCondition`, `OatType`, `NoOfOat`, `Status` FROM OatDis
 
Share this answer
 
Comments
Allysha April 23-Mar-21 11:02am    
Fatal error: Uncaught TypeError: mysqli_num_rows(): Argument #1 ($result) must be of type mysqli_result, bool given in C:\xampp\htdocs\PieChart\index.php:8 Stack trace: #0 C:\xampp\htdocs\PieChart\index.php(8): mysqli_num_rows(false) #1 {main} thrown in C:\xampp\htdocs\PieChart\index.php on line 8

this was what came up after changing that
Richard Deeming 23-Mar-21 11:04am    
Which simply means there was an error executing the query, which you didn't check for.

Most likely a typo in one of the column names.

Try executing the query directly in workbench to see what the error is.
Allysha April 23-Mar-21 11:08am    
thank you so much, it was one of the column names. it took me literally hours to debug

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900