943,175 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 85
  • PHP RSS
Sep 3rd, 2010
0

accessing specific record using php

Expand Post »
Hi all,

Newbie question;....
I have a database and a table within called 'data'..
I also have a login script that allows users to access it. My question is..how do I make only the user's record viewable/accessible after login. Specifically, how do I access, the a specific record in the database using php code??? ... Some of my variables are $email, $phone, $address.....etc..

I want to display only the users data when they login and I want then to be able to update it...

Hope you can help me a little..
Last edited by lwaterfo; Sep 3rd, 2010 at 3:05 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
lwaterfo is offline Offline
46 posts
since Jul 2010
Sep 3rd, 2010
0
Re: accessing specific record using php
When you set up your SQL statement add a WHERE clause to restrict it to the current user.

let's say that the logged on user is in the $user variable. you would code it something like this.

PHP Syntax (Toggle Plain Text)
  1. $sql = 'SELECT * FROM data WHERE userID ='" . $user ."'"
  2.  

You should probably substitute the field names you need for the *, but not knowing the layout of your data table this will retrieve everything from that table.

If you are using an MySQL database then you would want to execute the query like this:
PHP Syntax (Toggle Plain Text)
  1. //using mysqli functions
  2. $result = mysqli_query($sql);
  3.  
  4. // using mysql functions
  5. #result = mysql_query($sql);

You can find documentation and examples for mysqli functions here.
You can find documentation and examples for mysql functions here.
Reputation Points: 27
Solved Threads: 27
Junior Poster in Training
svilla is offline Offline
88 posts
since Aug 2010
Sep 3rd, 2010
0
Re: accessing specific record using php
Thanks very much. I think I am beginning to understand a little more...
One question..suppose I named the variable in my 'data' table as 'memberid'...
would I need to make the code read:
PHP Syntax (Toggle Plain Text)
  1. $sql = 'SELECT * FROM data WHERE memberid ='" . $memberid ."'"
  2.  
?

or is $user a variable that I can change the name of and have set to user input in the login page?

I am confused with the difference between 'userID' and '$user'...


_________________________________________________________________


Click to Expand / Collapse  Quote originally posted by svilla ...
When you set up your SQL statement add a WHERE clause to restrict it to the current user.

let's say that the logged on user is in the $user variable. you would code it something like this.

PHP Syntax (Toggle Plain Text)
  1. $sql = 'SELECT * FROM data WHERE userID ='" . $user ."'"
  2.  

You should probably substitute the field names you need for the *, but not knowing the layout of your data table this will retrieve everything from that table.

If you are using an MySQL database then you would want to execute the query like this:
PHP Syntax (Toggle Plain Text)
  1. //using mysqli functions
  2. $result = mysqli_query($sql);
  3.  
  4. // using mysql functions
  5. #result = mysql_query($sql);

You can find documentation and examples for mysqli functions here.
You can find documentation and examples for mysql functions here.
Reputation Points: 10
Solved Threads: 0
Light Poster
lwaterfo is offline Offline
46 posts
since Jul 2010
Sep 7th, 2010
0
Re: accessing specific record using php
Your coding example is correct as long as the variable $memberid holds the correct ID number for the current user/member.

In my example the userID is the column name in the database, the $user is a php variable that holds the user/memeber's id.

I am not sure where you are holding the user information once the user has signed in, but in order to have access to this information on every page and eve subsequent requests to the same page you will have to put this information into session or cookie variables. Session variables might make the most sense.

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. session_start(); // required for session variables
  3.  
  4. $_SESSION['memberid'] = $memberid; // Perform after user has successfully logged in
  5.  
  6. $memberid = $_SESSION['memberid'] // Perform whenever you need access to Member ID
  1. The session_start function is required to set up a session and access session variables.
  2. The first statement goes into your login script, after the user has provided a good password. You only need this once and you can use unset($_SESSION['memberid']), to remove the session variables when the user logs off.
  3. The second statement is used whenever you need to perform any tests based on the memberid. You can access the $_SESSION['memberid'] variable directly, but if you are going to use it more than once it is best to create a local variable.
Reputation Points: 27
Solved Threads: 27
Junior Poster in Training
svilla is offline Offline
88 posts
since Aug 2010
Sep 22nd, 2010
0
Re: accessing specific record using php
how would I display the results to the user?
I got the code running but don't know how to display anything..
Here is my entire code:
PHP Syntax (Toggle Plain Text)
  1.  
  2. <?php
  3. session_start();
  4.  
  5. if (!isset($_SESSION['memberusername'])){
  6. header("location:contractorlogin.php");
  7. exit();
  8. }
  9.  
  10. $user = $_SESSION['memberusername'];
  11. $sql = "SELECT Username FROM contractors WHERE Username LIKE'" . $user . "'";
  12. #result = mysql_query($sql);
  13. ?>
Reputation Points: 10
Solved Threads: 0
Light Poster
lwaterfo is offline Offline
46 posts
since Jul 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: php 5.2 works php 5.3 not working (upload images script)
Next Thread in PHP Forum Timeline: variable query issue





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC