MySQLi Count(*) Always Returns 1

MySQLi count(*) always returns 1

You have to fetch that one record, it will contain the result of Count()

$result = $db->query("SELECT COUNT(*) FROM `table`");
$row = $result->fetch_row();
echo '#: ', $row[0];

MySQLi - Always Returning 1 Row

It has been my experience that MySQL can return some funky values for row_count

maybe

 result = $db->query($mysqli, "SELECT COUNT(*) AS cnt FROM users WHERE username = '$user'")
while($row = $result->fetch_assoc()){
echo $row['username'] . '<br />';
}

or try

/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT Name FROM City LIMIT 10")) {
printf("Select returned %d rows.\n", $result->num_rows);

/* free result set */
$result->close();
}

http://php.net/manual/en/mysqli.query.php

Count always returns 1...but does it exist?

When you use COUNT() in your query you will always get one row returned even if only to tell you the count is zero. You need to check the value of COUNT(reviews) to get that value:

$result = mysqli_query($sqli, "SELECT count(reviews) AS `count` FROM myTable WHERE `category`='$list' AND `reviews`='$php_file_name'");
$check = mysqli_fetch_assoc($result);

if($check['count'] >= 1){

You'll notice that I gave count(reviews) an alias as it makes accessing that value easier in the PHP code.

Mysqli - num_rows always returns 1

When you do SELECT COUNT(*) there will always be at least one result. Even if its 0.

You will need to fetch the result of the query to get the correct count.

mysqli_num_rows returns 1 no matter what

When you use COUNT(*) you always get one row returned even if the count is zero.

You either:

  1. Want to remove the count(*) and then use mysqli_num_rows()
    or
  2. Get the result of count(*)

.

$row = mysqli_fetch_assoc($query);
echo $row['COUNT(`user_id`)'];

Why does mysqli_num_rows always return 1 when using SQL AVG() function?

I assume by incompatibility you mean that mysqli_num_rows() returns 1. It is because AVG() groups the rows into one and then computes the average.

If you want to get the real number of rows, you can use COUNT(*).

So, your query would be:

SELECT COUNT(*) as number_of_rows, avg(comp1) as competencia1 , avg(comp2) as competencia2 , avg(comp3) as competencia3 , avg(comp4) as competencia4 , avg(comp5) as competencia5  FROM votos WHERE topic_id = '$topic_id'

Then, you would use mysqli_fetch_row() or mysqli_fetch_assoc() to get the data.

$consulta_nota_geral = "SELECT COUNT(*) as number_of_rows, avg(comp1) as competencia1 , avg(comp2) as competencia2 , avg(comp3) as competencia3 , avg(comp4) as competencia4 , avg(comp5) as competencia5  FROM votos WHERE topic_id = '$topic_id'";
$consulta_nota_geral_conect = mysqli_query($conn,$consulta_nota_geral) or die (mysqli_error($conn));

$data = mysqli_fetch_assoc($consulta_nota_geral_conect);
if($data["number_of_rows"] > 0) {
//your code
}

php - MySQli row count always returning 0?

Your code is not very good...

You really need to be using MYSQLi prepared statements esp with username / pass checks...

$mysqli = new mysqli(...);
$query = "SELECT count(*) as count FROM `tblUser` WHERE `username`=? and `password`=?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('ss',$username,$password);
$stmt->execute();
$result = $stmt->get_result();

if($result->num_rows){
$row = $result->fetch_assoc();
echo $row['count'] . " rows in table tblUser.";
}

I understand you are potentially looking into just getting some information, but this is the best route to take so move this way. Do not continue in your path.

It is very likely that your query failed because you have ( ) around your actually query. So if you want to use that just get rid of the ( ) on the line with $queryLogin.



Related Topics



Leave a reply



Submit