www.webdeveloper.com
Page 1 of 4 123 ... LastLast
Results 1 to 15 of 46

Thread: Calling data from the database

  1. #1
    Join Date
    Aug 2012
    Location
    TX
    Posts
    293

    Calling data from the database

    Hello, I am new to PHP and have been working on a site which I would like to set up some user profiles. I have currently been toying around with the profile page and in order to get better equipped I have been trying to call different information to the tables on the profile page.

    I have not had much success. The only success I have is calling the username to the page with this,

    if ($_SESSION['username'])
    {
    echo "You are logged in as ".$_SESSION['username'];

    I have been trying to call the users email to the page with similar tags and tags I have found on the internet with no luck.

    Does anyone have a general rule of commands or some advice on how I can accomplish tasks as simple as calling information from the DB?

    I am at some point trying to set up a gallery for users to upload pictures to there profile and I really need some guidance on how to get there.

    Thanks!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,330
    Well...the information will only be in $_SESSION if your code put it there. So if you want to access it that way, you'll probably need to modify your login processing to add that data element to the $_SESSION array. Otherwise, you'll need to explicitly query the database for that info in any situation where you need it.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2012
    Location
    TX
    Posts
    293
    Ok this is what I have,


    <?php
    $db="login";
    $link = mysql_connect("localhost", "root", "");
    if (! $link)
    die("Couldn't connect to MySQL");
    mysql_select_db($db , $link)
    or die("Couldn't open $db: ".mysql_error());
    $result = mysql_query( "SELECT * FROM users" )
    or die("SELECT Error: ".mysql_error());
    $num_rows = mysql_num_rows($result);

    $i=0;


    while ($i<$num_rows )


    {
    $first=mysql_result($result, $i,"first");
    $last=mysql_result($result,$i,"last");
    $phone=mysql_result($result,$i,"phone");
    $mobile=mysql_result($result,$i,"mobile");
    $username=mysql_result($result,$i,"username");
    $email=mysql_result($result,$i,"email");
    $web=mysql_result($result,$i,"web");

    $i++;

    }
    echo "<b>Username: $username</b><br>Phone: $phone<br>Mobile: $mobile<br>E-mail: $email<br>Web: $web<br><hr><br>";&#160;




    mysql_close($link);
    ?>


    After this executes it post the most recent person who has registered on the sites information instead of the person who just loged in. I need this to show the persons information who is logged in. Thanks for your help...

  4. #4
    Join Date
    Aug 2012
    Posts
    155
    Well there are a couple of things wrong with your code.

    Firstly you should be selecting details from the database based on one user's information not all data (using their login details):

    PHP Code:
    $username mysql_real_escape_string(trim($_POST['username']));
    $password mysql_real_escape_string(trim($_POST['password']));
    $result mysql_query"SELECT * FROM users WHERE username='".$username."' AND password='".$password."' LIMIT 1"
    (if using SHA1 or MD5 to encrypt passwords in your database you need to use that to format your posted passwords)

    Then just fetch_array:

    PHP Code:
    while ($row mysql_fetch_array($result) {
      
    $_SESSION['username'] = $row['username'];
      
    $_SESSION['phone'] = $row['phone'];
      
    $_SESSION['mobile'] = $row['mobile'];
      
    $_SESSION['email'] = $row['email'];
      
    $_SESSION['web'] = $row['web'];

    You need to ensure you have
    PHP Code:
    session_start(); 
    at the top of every page where you want to use sessions (before any other php or html code).

    And then you can just echo it out:

    PHP Code:
    echo "<p><b>Username: $_SESSION['username']</b><br>Phone: $_SESSION['phone']<br>Mobile: $_SESSION['mobile']<br>E-mail: $_SESSION['email']<br>Web: $_SESSION['web']</p><hr>"

  5. #5
    Join Date
    Aug 2012
    Location
    TX
    Posts
    293
    This all makes sense to me. thank you.. but I am getting a syntax error here

    echo "<p><b>Username: $_SESSION['username']</b><br>Phone: $_SESSION['phone']<br>Mobile: $_SESSION['mobile']<br>E-mail: $_SESSION['email']<br>Web: $_SESSION['web']</p><hr>";

    This is the error

    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\pixel\profile.php on line 79
    Last edited by Nicholas Diaz; 08-26-2012 at 04:58 AM.

  6. #6
    Join Date
    Aug 2012
    Location
    TX
    Posts
    293
    Here is the whole code from top to bottom after updating with what you said. Please make sure I changed the correct locations.

    session_start();
    <?php
    $db="login";
    $link = mysql_connect("localhost", "root", "");
    if (! $link)
    die("Couldn't connect to MySQL");

    mysql_select_db($db , $link)
    or die("Couldn't open $db: ".mysql_error());


    $username = mysql_real_escape_string(trim($_POST['username']));
    $password = mysql_real_escape_string(trim($_POST['password']));
    $result = mysql_query( "SELECT * FROM users WHERE username='".$username."' AND password='".$password."' LIMIT 1")
    or die("SELECT Error: ".mysql_error());

    while ($row = mysql_fetch_array)($result);

    $_SESSION['username'] = $row['username'];
    $_SESSION['phone'] = $row['phone'];
    $_SESSION['mobile'] = $row['mobile'];
    $_SESSION['email'] = $row['email'];
    $_SESSION['web'] = $row['web'];







    echo "<b>Username: $_SESSION['username']</b><br>Phone: $_SESSION['phone']<br>Mobile: $_SESSION['mobile']<br>E-mail: $_SESSION['email']<br>Web: $_SESSION['web']<br><hr><br>";

    mysql_close($link);
    ?>

  7. #7
    Join Date
    Aug 2012
    Location
    TX
    Posts
    293
    Also maybe you can explain this to me... On my page called membersarea.php this is the code.


    <div id="usercontainer">
    <FONT color=#f80000 size=4 face="customfont">

    <?php

    session_start();

    if ($_SESSION['username'])
    {
    echo "You are logged in as ".$_SESSION['username'];
    echo "<p>";
    echo "<a href='logout.php'>Click to logout</a> &nbsp; <a href='profile.php'>Edit Profile</a>";

    }
    else
    header ("location: index.php");

    ?>
    </font>
    </div>


    </body>
    </html>

    How does it know the session username with out me defining any variables on this page.
    When I change the $_SESSION['username']; to $_SESSION['email']; I get a syntax error. How come it knows the username and works just fine on every page I include this file on but when I use email there instead it does not know it???
    Last edited by Nicholas Diaz; 08-26-2012 at 05:16 AM.

  8. #8
    Join Date
    Aug 2012
    Posts
    155
    Sorry, my fault, I forgot to format the echo properly. It should be:
    PHP Code:
    echo "<p><b>Username: " $_SESSION['username'] . "</b><br>Phone: " $_SESSION['phone'] . "<br>Mobile: " $_SESSION['mobile']. "<br>E-mail: " $_SESSION['email'] . "<br>Web: " $_SESSION['web']. "</p><hr>"

  9. #9
    Join Date
    Aug 2012
    Posts
    155
    Firstly, your
    PHP Code:
    session_start(); 
    should be before any other code on your page (PHP or HTML) so starting from line 1:
    PHP Code:
    <?php
      session_start
    ();
    ?>
    With regard to it not echoing out the email stored in the session, have you checked you are actually getting an email from the database and that it is being assigned correctly to the session variable:
    PHP Code:
    echo $row['email'];
    var_dump($_SESSION); // to view all session data, or
    var_dump($SESSION['email']) // to just view the email 

  10. #10
    Join Date
    Aug 2012
    Location
    TX
    Posts
    293
    Thank you that fixed the echo... but now when i load the page I get this error

    Notice: Undefined index: username in C:\xampp\htdocs\pixel\profile.php on line 60

    Notice: Undefined index: password in C:\xampp\htdocs\pixel\profile.php on line 61

    Notice: Use of undefined constant mysql_fetch_array - assumed 'mysql_fetch_array' in C:\xampp\htdocs\pixel\profile.php on line 65

    these are 60 and 61
    $username = mysql_real_escape_string(trim($_POST['username']));
    $password = mysql_real_escape_string(trim($_POST['password']));

    This is 65

    while ($row = mysql_fetch_array)($result);

  11. #11
    Join Date
    Aug 2012
    Posts
    155
    The undefined index problem generally means that no information is being assigned to the variables, possible a problem with your posted data. Try echoing out the posted data to see if they have any values.

    As for the while loop, what you have is incorrect but I also missed a closing bracket in my original code. It should be:
    PHP Code:
    while ($row mysql_fetch_array($result)) { 

  12. #12
    Join Date
    Aug 2012
    Location
    TX
    Posts
    293
    First off thank you so much for all your help... You really are helping me understand a ton and I can tell that this is really close to working.. I am now getting these errors...

    Notice: Undefined index: username in C:\xampp\htdocs\pixel\profile.php on line 60

    Notice: Undefined index: password in C:\xampp\htdocs\pixel\profile.php on line 61

    Notice: Undefined index: phone in C:\xampp\htdocs\pixel\profile.php on line 77

    Notice: Undefined index: mobile in C:\xampp\htdocs\pixel\profile.php on line 77

    Notice: Undefined index: email in C:\xampp\htdocs\pixel\profile.php on line 77

    Notice: Undefined index: web in C:\xampp\htdocs\pixel\profile.php on line 77

    This is line 77
    echo "<p><b>Username: " . $_SESSION['username'] . "</b><br>Phone: " . $_SESSION['phone'] . "<br>Mobile: " . $_SESSION['mobile']. "<br>E-mail: " . $_SESSION['email'] . "<br>Web: " . $_SESSION['web']. "</p><hr>";

    I am pretty sure this is because I am not assigning the variables right when entering the data in to the DB.

  13. #13
    Join Date
    Aug 2012
    Posts
    155
    That is what I said before, echo out the data you are fetching from your database to ensure you are getting it and the data is correct. So as a temporary measure, in your while loop, comment out assigning to the sessions and echo each row individually.
    PHP Code:
     while ($row mysql_fetch_array($result)) { 
      
    //$_SESSION['username'] = $row['username']; 
      //$_SESSION['phone'] = $row['phone']; 
      //$_SESSION['mobile'] = $row['mobile']; 
      //$_SESSION['email'] = $row['email']; 
      //$_SESSION['web'] = $row['web']; 
      
    echo $row['username'];

    Run this and see if you have a value for the username and that it is correct. If so change the echo statement to the next value (in this case phone) and run again and so on.

  14. #14
    Join Date
    Aug 2012
    Location
    TX
    Posts
    293
    I am assuming this is because I have a registration.php posting data in the data base and then assigning variables.
    a login.php doing the same

    and then we are working in a profile.php file currently.

    So basically what I need to do is make sure the data when posted in the data base has a variable attached to each row.

    Then when we log in make sure this information matches.

    Then in the profile page in order to call this information make sure they are calling the right variables assigned in the previous files??? From what I am gathering this is how it works???

    For instance, when you added $_SESSION its cause you assume based on what I originally sent you that I have already set SESSION up as the variable to call on this information???

  15. #15
    Join Date
    Aug 2012
    Posts
    155
    Actually, looking at the code again, the problem is still your data being submitted by your form. What is you form code?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles