Jump to content
Sign in to follow this  
pemoamsi

Sqlite problemas no cout

Recommended Posts

pemoamsi

Algo aqui não bate bem:

SELECT COUNT(*) FROM users; <- Executada no programa (77 resultados)

               	 $selection = $db->prepare('SELECT COUNT(*) FROM users;');
                   $selection->execute();
                   $count = $selection->rowCount();

0 resultados... alguém sabe identificar o que estou a fazer mal?

Share this post


Link to post
Share on other sites
pemoamsi

tens a ligação correctamente efectuada ?

Sim, estou do mesmo modo a efectuar um insert:

                   	 $result = $db->query('insert into users
                                       values (NULL,
                                               "'.$user.'",
                                               "'.$password.'",
                                               "'.$nivel.'")');

A questão é que queria verificar se tal user já existia e caso contrário registar... mas consigo sempre... pois o cont dá 0 :(

Share this post


Link to post
Share on other sites
HappyHippyHippo

não estou a ver que abstração estás a usar, mas não deverias guardar o resultado do query e verificar o número de registos no objecto retornado ?


IRC : sim, é algo que ainda existe >> #p@p

Share this post


Link to post
Share on other sites
pemoamsi

não estou a ver que abstração estás a usar, mas não deverias guardar o resultado do query e verificar o número de registos no objecto retornado ?

<?php
$db = new PDO('sqlite:backup.sqlite');
$result = $db->prepare('SELECT * FROM users');
$result->execute();
$count = $result->rowCount();
print $count;
?>

Resultado: 0

<?php
$db = new PDO('sqlite:backup.sqlite');
$result = $db->query('SELECT * FROM users');
$count = $result->rowCount();
print $count;
?>

Resultado

Resultado da query directamente na base de dados: 8

Edited by pemoamsi

Share this post


Link to post
Share on other sites
HappyHippyHippo

http://www.php.net/manual/en/pdostatement.rowcount.php

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.


IRC : sim, é algo que ainda existe >> #p@p

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.