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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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.