0
include_once "register.php";

if (isset($_POST["username"])) {
    $username = $_POST["username"];
    $password = $_POST["password"];
    // $remember = $_POST["remember"];
    $pdo = connect_db();
    $query = $pdo->prepare("SELECT * FROM user_db WHERE username='$username'");
    $res = $query->execute();
brombeer
  • 8,716
  • 5
  • 21
  • 27
  • This Code return Type Boolean I want return Array When i want to check the condition i have a error and it say Warning: Trying to access array offset on value of type bool in C:\xampp\htdocs\Login-5\index.php on line 23 –  May 02 '22 at 05:48
  • **Warning!** You're open to [SQL injection attacks](https://owasp.org/www-community/attacks/SQL_Injection)! Read [how to prevent SQL injection in PHP](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) by using prepared statements correctly with bound parameters instead of injecting variables directly into your queries. It's not just about security. If your data contains, for example, a single quote `'`, your query will break. – M. Eriksson May 02 '22 at 06:13

1 Answers1

1

use this code it will return array value in print_r here you have to change "dbname" to your database name.

<?php
    
    $servername = "localhost";
    $username = "root";
    $password = "";
    
    try {
      $conn = new PDO("mysql:host=$servername;dbname=mydb", $username, $password);
      // set the PDO error mode to exception
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      echo "Connected successfully";
    } catch(PDOException $e) {
      echo "Connection failed: " . $e->getMessage();
    }
    
    $username = $_POST["username"];
        //$password = $_POST["password"];
        // $remember = $_POST["remember"];
        //$pdo = connect_db();
    
        
        $stmt1=$conn->prepare('SELECT * FROM user_db WHERE username=:username');
        $stmt1->bindparam(":username", $username);
        
        $stmt1->execute();
        
        $row = $stmt1->fetch();
        print_r($row);
        echo $row['username'];
    ?>
Dotsquares
  • 755
  • 1
  • 2
  • 10