1

I would like to display the users data from the table, so once the user has logged in to the website, text shows on the page saying "Welcome (firstname) (lastname)."

The columns for firstname is "Firstname" and lastname is "Lastname". I have tried many different methods, but all of them seem to return errors and I cannot get it to work. If anyone can help, that would be great.

Here is my code:

<html> <head> <title>Landing page</title> <link rel="stylesheet" type="text/css" href="css.css"> </head> <body> <?php /* ESTABLISH FIRST YOUR CONNECTION TO YOUR DATABASE */ $con = new mysqli("localhost", "root", "", "user_login"); /* REPLACE NECESSARY DATA */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } if($stmt = $con->prepare("SELECT Username, Password FROM users WHERE Username = ? AND Password = ?")){ $stmt->bind_param("ss",$_POST["username"],$_POST["password"]); $stmt->execute(); $stmt->store_result(); $checklog = $stmt->num_rows; if($checklog > 0){ /* HERE IS YOUR CODE WITH SUCCESSFUL LOGIN */ session_start(); $_SESSION["logged_in"] = "YES"; echo "<h1>You are now logged in</h1>"; echo "<p><a href='secure1.php'>Link to protected file</a></p>"; echo "<p><a href='secure2.php'>Link to protected file #2</a></p>"; } else { /* HERE IS YOUR CODE WITH UNSUCCESSFUL LOGIN */ session_start(); $_SESSION["logged_in"] = "NO"; echo "<h1>You are NOT logged in </h1>"; echo "<p><a href='secure1.php'>Link to protected file</a></p>"; echo "<p><a href='secure2.php'>Link to protected file #2</a></p>"; } $stmt->close(); } ?> <p><a href="public.html">Public Page</a></p> <p><a href="logout.html">Logout</a></p> </body> </html> 

What is the mistake i am doing and how can i fix it ?

2
  • Where is your code you have tried for echoing "Welcome (firstname) (lastname)."? and what error you are getting? Commented Jun 17, 2015 at 8:40
  • You need to fetch Firstname and Lastname in your query and then print them by using result variable Commented Jun 17, 2015 at 8:43

2 Answers 2

1

You can do $stmt->fetch(PDO::FETCH_ASSOC)

And store your Result in the $result

$result = $stmt->fetch(PDO::FETCH_ASSOC) $Firstname = $result["Firstname"]; 

As you might have only one matching row, I would recommend you have to have your condition like $checklog==1

So, You shall have something like this

$checklog = $stmt->num_rows; if($checklog == 1) // Allows only for one match { session_start(); $_SESSION["logged_in"] = "YES"; $result = $stmt->fetch(PDO::FETCH_ASSOC); // Fetch the row into $result $Firstname = $result["Firstname"]; // This is your First Name $Lastname = $result["Lastname"]; // This is your Last Name } 

You can have the First Name and Last Name in your session like this

$_SESSION["FirstName"] = $result["Firstname"]; $_SESSION["LastName"] = $result["Lastname"]; 

Update :

Here's the cool and entirely updated version that is hand made just for you :)

It took my cool 10 minutes to construct it :)

<?php $conn = new PDO("mysql:host=localhost;dbname=user_login",'root',''); $name = $_POST["username"]; $pass = $_POST["password"]; $sql = "SELECT * FROM users WHERE Username = ? AND Password = ?"; $q = $conn->prepare($sql); $q->execute(array($name,$pass)); $count = $q->rowCount(); if ($count==1) { $q->setFetchMode(PDO::FETCH_BOTH); while($row = $q->fetch()) { echo $row['FirstName']; //Just to show you output session_start(); $_SESSION["FirstName"] = $row['FirstName']; } } ?> 
Sign up to request clarification or add additional context in comments.

14 Comments

It is displaying an error: syntax error, unexpected '$Firstname' (T_VARIABLE). Sorry, I am still learning PHP
Sorry, Its my fault i missed the ; in the line $result = $stmt->fetch(PDO::FETCH_ASSOC); I have updated it , can you try with this ?
Thanks for all of your help; however a new error stating: "Warning: mysqli_stmt::fetch() expects exactly 0 parameters, 1 given" on line 27:"$result = $stmt->fetch(PDO::FETCH_ASSOC); // Fetch the row into $result" Thanks
I have updated my answer, You can check it now.. Sorry for late reply :)
I am not sure whether you have FirstName or firstname inside your database, You can change it accordingly :)
|
0

try this..

<?php if($stmt = $con->prepare("SELECT firstname,lastname,Username, Password FROM users WHERE Username = ? AND Password = ?")){ $stmt->bind_param("ss",$_POST["username"],$_POST["password"]); $stmt->execute(); //$stmt->store_result(); //$checklog = $stmt->num_rows; $results = $stmt->fetch(PDO::FETCH_ASSOC); $_SESSION['firstname'] = $results['firstname']; $_SESSION['lastname'] = $results['lastname']; print_r($_SESSION); 

1 Comment

Hi I am getting the error: "Warning: mysqli_stmt::fetch() expects exactly 0 parameters, 1 given" on the line: "$results = $stmt->fetch(PDO::FETCH_ASSOC);", Sorry if I have got something wrong, I am quite new to PHP

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.