Click here to Skip to main content
15,868,164 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.
PHP
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  
  $email = 
  $password =
  $row = '';

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

  else 
  {

    $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);

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


html code
HTML
 <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>
    <div class="form-filed">
      <label for="password">Password:</label>
      <input type="password" name="password" id="password" placeholder="Enter password">
    </div>
    <input type="submit" value="Login" class="btn">
  </form>
</section>


What I have tried:

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

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
 
Comments
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