SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting data from MySql display as variables

    Hi, I'm a newbie to this but I would like to use sessions and the person logs in then they go to another page, and I have just coded an example of what I want. Basically it selects the email address and ipaddress and date from the database where the user name is $varified_user (which comes from the session) and displays the information. This is a simplified version of what I want to do. Here is the main code, can you help, cos I'm lost? :
    PHP Code:
    //Query the database for user details using the session variable $verified_user

    $result = @mysql_query("SELECT Email_Add, ipaddress, Date FROM users
                WHERE User_Name='
    $verified_user'");
        if(@
    mysql_num_rows($result) != 0
        {
                
    $email_add $result[Email_Add];
            
    $ipaddr    $result[ipaddress];
            
    $date $result[Date];
            echo 
    "Your email address is: $email_add";
            }

    print 
    "You are Logged in as: $verified_user";

    echo 
    "<BR> Hi There, these are your user details";
    echo 
    "<BR>Your email address is: $email_add";

    ?> 

  2. #2
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The query returns a result identifier, but not the actual values that exist in the database. You need to use a specialized function to read the contents of the result identifier into actual values that you may use. Try this:
    PHP Code:
    <?php
    //Query the database for user details using the session variable $verified_user
    $query=@mysql_query("SELECT Email_Add, ipaddress, Date FROM users ".
        
    "WHERE User_Name='$verified_user'");
    $result=mysql_fetch_assoc($query);
    if(@
    mysql_num_rows($result) != 0)
    {
        
    $email_add $result['Email_Add'];
        
    $ipaddr $result['ipaddress'];
        
    $date $result['Date'];
        echo 
    "Your email address is: $email_add";
    }
    print 
    "You are Logged in as: $verified_user";
    echo 
    "<br /> Hi There, these are your user details";
    echo 
    "<br />Your email address is: $email_add";
    ?>
    I also added single quotes around the array elements as that's the proper way to use them.

    -Colin

  3. #3
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This always works for me
    PHP Code:
    <?php
    $s 
    = @mysql_query("SELECT Email_Add, ipaddress, Date FROM users WHERE User_Name='$verified_user'");
    if (
    $s) {
      while (
    $r mysql_fetch_object($s)) {
        
    $email_add $r->Email_Add;
        
    $ipaddr $r->ipaddress;
        
    $date $r->Date;
        echo 
    "You are Logged in as: " .$verified_user"\n";
        echo 
    "<BR> Hi There, these are your user details\n";
        echo 
    "<BR>Your email address is: " .$email_add"\n";
        echo 
    "Your email address is: " .$email_add"\n";
      }
    }
    ?>
    Edit:

    Sorry I guess I missed that when I copied the code
    Last edited by notepad_coder; Jun 20, 2002 at 20:23.

  4. #4
    SitePoint Evangelist cyngon's Avatar
    Join Date
    Aug 2001
    Location
    Livonia, MI, USA
    Posts
    513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    notepad_coder,

    On lines 4, 5, and 6, of the code you posted, are the ]'s supposed to be there?

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi All, Thanks for your advice, I tried typing in both and got errors, anyway I copied and pasted the two different answers, that you gave, but neither if them will display the email address from the database???

    Also could you tell me what's the difference between putting: echo "Your email address is: " .$email_add. "\n"; and putting: echo "Your email address is: $email_add";
    What does the extra full stop do?

    One other thing, I was trying to display the IPaddress and I just typed in the same thing: echo "<BR>Your IPaddress is : $ipaddress";
    I put it inside the { } and refreshed the page, but the new line wasn't diplayed.
    When I put it outside the { } just before the ?> it would display the line, but again not the actual value from the database (it just leaves a blank)

    Hope someone can help me solve this, Thanks!

  6. #6
    SitePoint Zealot Klav's Avatar
    Join Date
    Mar 2002
    Location
    Huddersfield, UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Navelly
    Also could you tell me what's the difference between putting: echo "Your email address is: " .$email_add. "\n"; and putting: echo "Your email address is: $email_add";
    What does the extra full stop do?
    The extra full stops concatenate (join) the text with the variable. This isn't needed because quotes handle variables anyway. You should use it, for example, if say you want to include something like $_SERVER['PHP_SELF'] in a string.

    Originally posted by Navelly
    One other thing, I was trying to display the IPaddress and I just typed in the same thing: echo "<BR>Your IPaddress is : $ipaddress";
    I put it inside the { } and refreshed the page, but the new line wasn't diplayed.
    When I put it outside the { } just before the ?> it would display the line, but again not the actual value from the database (it just leaves a blank)?
    That's because you haven't done anything to fetch the array/object from outside the curly braces. Use Notepad_Coder's mysql_fetch_object method to display the data or, if you want a change, use mysql_fetch_array.

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oooops!
    I'm very sorry, but I just checked the database and there was nothing in the email address field, added it in and notepad_coder's code worked, but strangely Aes' code wont display anything.
    Ah! Wel I'll use notepad_coder's,
    Thanks for your help!!!

    quote:
    --------------------------------------------------------------------------------
    Originally posted by Navelly
    One other thing, I was trying to display the IPaddress and I just typed in the same thing: echo "<BR>Your IPaddress is : $ipaddress";
    I put it inside the { } and refreshed the page, but the new line wasn't diplayed.
    When I put it outside the { } just before the ?> it would display the line, but again not the actual value from the database (it just leaves a blank)?
    --------------------------------------------------------------------------------


    That's because you haven't done anything to fetch the array/object from outside the curly braces. Use Notepad_Coder's mysql_fetch_object method to display the data or, if you want a change, use mysql_fetch_array.
    This works now too!

  8. #8
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In my code this line:
    PHP Code:
    if(@mysql_num_rows($result) != 0
    Should be:
    PHP Code:
    if(@mysql_num_rows($query) != 0
    That will fix the problem. Actually, my coding will be more efficient than notepad_coder's in that it doesn't resort to OOP capabilities (PHP doesn't support OOP well at all yet). When you upgrade to PHP version 4.3 either should work about the same.

    -Colin
    Last edited by Aes; Jun 21, 2002 at 16:13.


Bookmarks

Posting Permissions

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