Jump to content

Recommended Posts

Hi, how would I make it so that my page returns and echos database information? I've used the mysqli method in most of my coding, examples below and basically I want it so that it querys the firstname and lastname to the correct session id. I managed to do something like this before by doing a $row search but the problem with that was that it was returning all of the first and lastnames not one that matches the id to the database. I know you can do $row searches but I'm not too sure on those and the ones that were shown are not safe as it displays things like the database name, database password ect in that document where I've done an include like > include 'dbConfig.php';

 

(Also please note even though it hasn't got any includes, that's because the pages themselves are includes and all the includes that are needed such as the database one, I've declared on previous pages.)

 

How I would like it to work:

<?php
  
   Do some kind of safe row search up here.




    if(<USERS ID IS VALID THEN DO THIS>)
     {
	Echo the firstname and lastname here.
     }
?>

 

 

 

How I've tried to do it:

 


<?php
 
 if ($stmt = $mysqli->prepare("SELECT id, firstname, lastname FROM users WHERE firstname = ? AND lastname = ? LIMIT 1")) { 
      $stmt->execute();
      $stmt->store_result();
      $stmt->bind_result($user_id, $firstname, $lastname);
      $stmt->fetch();
 
      if($stmt->num_rows == 1) {
               $_SESSION['user_id'] = $user_id; 
               $_SESSION['firstname'] = $firstname;
               $_SESSION['lastname'] = $lastname;
			   
			   echo $firstname;
			   echo $lastname;
			   
               return true;    
		}
}
           
	  
?>

 

 

 

You are trying to return the users "firstname" and "lastname", by querying their "firstname" and "lastname".

Should you be using the "id" to return the data you want.

 

Use the $user_id variable to search the database and return the data.

You are trying to return the users "firstname" and "lastname", by querying their "firstname" and "lastname".

Should you be using the "id" to return the data you want.

 

Use the $user_id variable to search the database and return the data.

 

Yeah, I wanted it so that when they login it returns their First and Lastname depending on the ID.

The WHERE in your query should be searching against id and not firstname and lastname, you need to change it.

 

Ok so instead of this:

 

"SELECT id, firstname, lastname FROM users WHERE firstname = ? AND lastname = ? LIMIT 1"

 

 

I should change it to this?:

 

"SELECT id, firstname, lastname FROM users WHERE id = ? LIMIT 1"

Give it a try? :)

 

Aha done it. I removed the = ?, my fault, was being stupid xD Thanks for your help ^.^

 

 

 

EDIT:

 

Ok I've noticed a problem, it's returning only 1 record, so i'm only getting one name, its not getting the name depending on the ID :/

Edited by Hazukiy

When using "WHERE id = ?" you need to bind the id to ?.

 

Where is the user's id coming from? A session variable?

 

Well I have this as my session but I would like it to get the name through the database ID. So when they login, it's getting their database ID.

 

function sec_session_start() {
        $session_name = 'sec_session_id';
        $secure = false;
        $httponly = true;
 
        ini_set('session.use_only_cookies', 1); 
        $cookieParams = session_get_cookie_params();
        session_set_cookie_params($cookieParams["lifetime"], $cookieParams["path"], $cookieParams["domain"], $secure, $httponly); 
        session_name($session_name);
        session_start();
        session_regenerate_id(true);     
}

You should have explained your question a bit better, but we got their in the end.

 

You need to create a log in form, then check their log in details against the database.

 

I assumed you had them logged in, but couldn't get the data you wanted back.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.