-1

I'm connecting to a BD to get some data using PHP. I'm using the following code to get data from users table:

if ($query = $db -> prepare ("SELECT id, name, email, password FROM `users` WHERE email = ?")) {
        $query -> bind_param('s',$email);
        $query -> execute();
        $row = $query-> fetch();
        $columnas = $query-> num_rows;
        if ($row) {
            echo 'row es true';
        }
        if (!$row) {
            echo 'row es false';
        }

Everything works good but I get a boolean value of $row (True if I have any result, False If not). I would like to get for example the value password of my query but I can't.

What am I doing wrong?

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • `fetch()` just returns a boolean to indicate whether a row could be fetched. You need to use `bind_result()` to bind the columns to variables that contain the results. – Barmar Aug 25 '21 at 22:58
  • See the examples in the documentation. – Barmar Aug 25 '21 at 23:00
  • If you are only starting to learn PHP then you should learn PDO instead of mysqli. PDO is much easier and more suitable for beginners. Start here https://phpdelusions.net/pdo & https://websitebeaver.com/php-pdo-prepared-statements-to-prevent-sql-injection – Dharman Aug 26 '21 at 10:54

1 Answers1

-1

You are about to get data from db and you need to use pdo fetch method for getting data. Modify your code with below one.

if ($query = $db -> prepare ("SELECT id, name, email, password FROM `users` WHERE email = ?")) {
        $query -> bind_param('s',$email);
        $query -> execute();
  
        $columnas = $query-> num_rows;
        if ($row > 0 ) {
           $row = $query-> fetch(PDO::FETCH_ASSOC);
print_r($result);
print("\n");
            echo 'row es true';
        }else{ 
            echo 'row es false';
        }
Ariful Islam
  • 696
  • 6
  • 11