-2

Is there any mistake in this code ? or my error is somewhere else .

$conn = mysqli_connect("localhost","root","root","sajan")
or die("Unable to connect to MySQL");   
$sql_count = "SELECT count(*) FROM user";
$result = $conn->query($sql_count);
$total_row = $result_count->num_rows;
Dharman
  • 30,962
  • 25
  • 85
  • 135
Sajan
  • 1,893
  • 2
  • 19
  • 39
  • Your query returns exactly one row, the first column of which is the count of rows in the table. You need to `fetch` the row and read the column. –  Mar 20 '15 at 06:26
  • because you are calculating count, obviously you will get a single row containing the count – Kuldeep Dangi Mar 20 '15 at 06:26

3 Answers3

1

Yes count will aggregate and give you only 1 value,You need to do this instead

$sql_count = "SELECT count(*) as total FROM user";
$result = $conn->query($sql_count);
$arrayresult = $result->fetch_array();
echo $arrayresult['total]
rahulinaction
  • 344
  • 4
  • 14
0

if you need both data and results count you must use this

$conn = mysqli_connect("localhost","root","root","sajan")
or die("Unable to connect to MySQL");   
$sql_count = "SELECT * FROM user";
$result = $conn->query($sql_count);
$total_row = $result->num_rows;

if you just need the count rahul solution is good and optimal that is

$sql_count = "SELECT count(*) as total FROM user";
$result = $conn->query($sql_count);
$arrayresult = $result->fetch_array();
echo $arrayresult['total]
Kuldeep Dangi
  • 4,126
  • 5
  • 33
  • 56
-3
$conn = mysqli_connect("localhost","root","root","sajan")
or die("Unable to connect to MySQL");   
$sql_count = "SELECT count(*) FROM user";
$result = $conn->query($sql_count);
$total_row = $result->num_rows;

will do the trick, you're referencing $result_count; which is not set.

Dharman
  • 30,962
  • 25
  • 85
  • 135