Click here to Skip to main content
15,667,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using php 8 and I am trying to fetch data from my database using the fetch method. However, nothing seem to be working.
  $email = 
  $password =
  $row = '';

  if (empty($_POST['email']) || empty($_POST['password']))
    echo "<span>All fileds are required</span>";


    $email = $_POST['email'];
    $password = $_POST['password'];

    $query = " SELECT email FROM register_user WHERE email =:email";
    $statement = $pdo->prepare($query);
    $statement->bindParam(':email', $email, PDO::PARAM_STR);

      $row = $statement->fetch(PDO::FETCH_ASSOC);
       $firstName = $row['first_name'] ?? ' ';
       echo $firstName;
       echo "failed";

html code
 <section class="form-container">
  <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">
    <div class="form-filed">
      <label for="email">Email:</label>
      <input type="email" name="email" id="email" placeholder="Enter email">
    <div class="form-filed">
      <label for="password">Password:</label>
      <input type="password" name="password" id="password" placeholder="Enter password">
    <input type="submit" value="Login" class="btn">

What I have tried:

$firstName = $row['first_name'];
echo $firstName;
Updated 7-Jan-23 6:44am

1 solution

Look at your code:
$query = " SELECT email FROM register_user WHERE email =:email";

That returns a single column (and hopefully a single row) called "email".

So you can't ask for any other column from that dataset!

Probably, you want to fetch the "first_name" column instead? (Or more likely, several columns.)
Share this answer
Member 14028652 7-Jan-23 12:38pm    
$query = " SELECT * FROM register_user WHERE email =:email" is now working and thank you.
OriginalGriff 7-Jan-23 12:44pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900