0

I'm trying to move to PDO.

I want to do a classic query for checking number of rows, in mysql I would have done mysql_num_rows_result but I've read somewhere that in PDO it does not work.

So I've tried with a fetch column using the following code :

global $bdd;
            $login = $bdd->query("SELECT
                   COUNT
                    FROM spb_clients_acces_compte,
                         spb_clients_infos_generales
                    WHERE spb_clients_acces_compte.spb_clients_acces_compte_e_mail = '{$email}'
                    AND spb_clients_acces_compte.spb_clients_acces_compte_pwd_md5 = '{$pass}'
                    AND spb_clients_acces_compte.spb_clients_acces_compte_admin = 1");
            $reponse = $bdd->fetchColumn();
            if ($reponse == 0):

            elseif ($reponse == 1):

            else:
            endif;
            var_dump($reponse);

This return to me :

( ! ) Fatal error: Call to undefined method PDO::fetchColumn() in C:\wamp\www\spb2016\admin\core\login.php on line 20

Line 20 is : $reponse = $bdd->fetchColumn();

I've tried many things with no success

Anykind of help will be much appreciated.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Stanislas Piotrowski
  • 2,595
  • 10
  • 40
  • 59
  • *"I would have done mysql_num_rows_result but I've read somewhere that in PDO it does not work."* - Exactly. You can't go mixing different APIs and we don't know if you're using the same one to connect with. If you're not connecting with PDO, well that's an issue. – Funk Forty Niner Apr 22 '16 at 16:25
  • [Get the number of rows with PDO](http://stackoverflow.com/a/31569733/1011527) – Jay Blanchard Apr 22 '16 at 16:25
  • This `SELECT COUNT` if you're wanting to use that function and you don't have a column called `COUNT`, then it's `COUNT(col|*)` – Funk Forty Niner Apr 22 '16 at 16:26
  • connexion is `$bdd = new PDO("mysql:host=localhost;dbname=183867brwzl", "root", "");` – Stanislas Piotrowski Apr 22 '16 at 16:27
  • 1
    RTFM: http://php.net/manual/en/pdostatement.fetchcolumn.php `fetchColumn` is a method of the PDOStatement object, which is what you get BACK from your query call. `$bdd` is a PDO object, `$login` is a PDOStatement. – Marc B Apr 22 '16 at 16:27
  • Sidenote: `compte_pwd_md5 = '{$pass}'` - MD5, really? You're using PDO so you must be able to use `password_hash()`, which you should. – Funk Forty Niner Apr 22 '16 at 16:31
  • You really shouldn't use [MD5 password hashes](http://security.stackexchange.com/questions/19906/is-md5-considered-insecure) and you really should use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. Make sure that you [don't escape passwords](http://stackoverflow.com/q/36628418/1011527) or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard Apr 22 '16 at 16:35
  • it is to simplyfy I have some sha1 but I removed it when I post here – Stanislas Piotrowski Apr 22 '16 at 16:35

1 Answers1

1

First of all, you need to add a column to your COUNT function. COUNT is a mysql function that returns the number of whatever parameter you pass it, so COUNT(id) would work.

Second, you must use the PDOStatement in order to fetch results. Replace

$reponse = $bdd->fetchColumn();

with

$reponse = $login->fetchColumn();
Technoh
  • 1,606
  • 15
  • 34