I'm trying to do a SELECT COUNT(*) with Postgres.

What I need: Catch the rows affected by the query. It's a school system. If the student is not registered, do something (if).

What I tried:

$query = pg_query("SELECT COUNT(*) FROM inscritossimulado
            WHERE codigo_da_escola = '".$CodEscola."'
            AND codigo_do_simulado = '".$simulado."'
            AND codigo_do_aluno = '".$aluno."'");

if(pg_num_rows($query) == 0)
{
    echo "Error you're not registered!";
}
else
{
    echo "Hello!";
}

Note: The student in question IS NOT REGISTERED, but the result is always 1 and not 0.

For some reason, when I "show" the query, the result is: "Resource id #21". But, I look many times in the table, and the user is not there.

有帮助吗?

解决方案

You are counting the number of rows in the answer, and your query always returns a single line.

Your query says: return one row giving the number of students matching my criteria. If no one matches, you will get back one row with the value 0. If you have 7 people matching, you will get back one row with the value 7.

If you change your query to select * from ... you will get the right answer from pg_num_rows().

其他提示

Actually, don't count at all. You don't need the count. Just check for existence, which is proven if a single row qualifies:

$query = pg_query(
  'SELECT 1
   FROM   inscritossimulado
   WHERE  codigo_da_escola = $$' . $CodEscola . '$$
   AND    codigo_do_simulado = $$' . $simulado. '$$
   AND    codigo_do_aluno = $$' . $aluno . '$$
   LIMIT  1');

Returns 1 row if found, else no row. Using dollar-quoting in the SQL code, so we can use the safer and faster single quotes in PHP (I presume).

The problem with the aggregate function count() (besides being more expensive) is that it always returns a row - with the value 0 if no rows qualify.

But this still stinks. Don't use string concatenation, which is an open invitation for SQL injection. Rather use prepared statements ... Check out PDO ...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top