-9

I'm trying to count all my project in project table Project_Name is a column_name

Here is the my code I have tried:

<?php
   $sql = "SELECT COUNT(*) FROM project";
   $result = $connection->query($sql);  
   if ($result->num_rows > 0) {
      $row = $result->fetch_assoc();    
      $project_count = $row['Project_Name'];
   } 
   else {
      echo "0 results"; 
   } 
?>
Saclt7
  • 377
  • 1
  • 8
  • 32
Derek Anas
  • 37
  • 9

2 Answers2

0

Try this

$sql = "SELECT COUNT(*) FROM project";
$result = $connection->query($sql); 
if ($result->num_rows > 0) {
    $row =$result->fetch_array();
    $project_count = $row[0];
}
Val
  • 207,596
  • 13
  • 358
  • 360
  • 2
    `mysql_fetch_array` you can't mix `mysql_` functions with anything other than their own. That should read as `$result->fetch_array()` since they're using an object oriented method to connect with. – Funk Forty Niner May 14 '15 at 04:32
  • `num_rows` doesn't make sense, it will always yield a row, just get the count – Kevin May 14 '15 at 04:35
  • thanks it works but i found way out by echo $project_count[0]; with an array but it doesnt make sense but this one should work thanks a lot! – Derek Anas May 14 '15 at 05:32
0

The query is returning only the count. Use another query like: SELECT * FROM project; for fetching the columns values in each row. With this, you will be able to do $row['Project_Name'];

Val
  • 207,596
  • 13
  • 358
  • 360
shruti1810
  • 3,920
  • 2
  • 16
  • 28