SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I don't understand what this does $row[ ]

    I have seen this code and have even got some of it to work, but I do not understand what it does.
    PHP Code:
                    if($_SESSION['token_id'] == $row[0]) {
                        echo 
    ", {$_SESSION['first_name']}!";
                        
    $loggedin 1;
                        }
                        else {
                        echo 
    ", please login.";
                        
    $loggedin 0;
                        } 
    In this example there is $row[0] but in other parts of the code there are different numbers. What is the significance of the number between the [ and the ] ?

    I have spent the last several hours reading the online PHP and MySQL manuals without finding the information.
    Each day is a learning experience.

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,608
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    $row is an array - it can therefore contain many different values all at the same time with one value stored in the first position $row[0], a different value in the second position $row[1] and so on for as many values as $rows stores.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am assuming $row was created from something like

    PHP Code:
    $row mysql_fetch_array($result); 
    where $result is the output result set from a database query.

    If so, then say each row of data in $result contains 3 columns of data - firstName, lastName, emailAddress.

    $row[0] will refer to the first column's data - firstName

    $row[1] will refer to the secon column's data - lastName

    and so on.

    Instead of using an indexed array you could use mysql_fetch_assoc() instead which returns an associative array for $row. In this case you can refer to the column names by their actual returned names in $result.

    So the value for firstName in a row could be referenced with $row['firstName']

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by webdev1958 View Post
    Instead of using an indexed array you could use mysql_fetch_assoc() instead which returns an associative array for $row. In this case you can refer to the column names by their actual returned names in $result.

    So the value for firstName in a row could be referenced with $row['firstName']
    fetch_array without qualifier flags returns both numerical and associative entries.

    Though it is more efficient to use assoc, or flag the fetch_array to return only assoc.

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,907
    Mentioned
    139 Post(s)
    Tagged
    2 Thread(s)
    Don't ever use these numeric indices unless you like debugging sessions that last for several hours.
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  6. #6
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So I could save myself a lot of trouble if I made my code read:
    PHP Code:
    if($_SESSION['token_id'] == $row['token_id"]) {
                        echo ", {$_SESSION['
    first_name']}!";
                        $loggedin = 1;
                        } 
    But as part of my learning:
    In a previous script, I selected 5 items from the MySQL database and then added the token_id to the array. Would this make token_id be at $row[5]?
    Each day is a learning experience.

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Depends on how you added it to the array.

  8. #8
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How can I read the array?

    Can I
    echo $row;
    to see what is in the array?
    Each day is a learning experience.

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,052
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    echo '<pre>',print_r($data),'</pre>';
    The only code I hate more than my own is everyone else's.

  10. #10
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried the
    echo '<pre>',print_r($data),'</pre>';
    and all I got was 1.

    I then tried
    echo '<pre>',print_r($row),'</pre>';
    and then I got:
    Array
    (
    [0] => 1018860
    )
    1

    I don't know what all that means, but by setting my code to $row[0] I got it working.

    I am guessing that the only item in the array is [0] => 1018860 and the final item is a 1 indicating there is only one value in the array.

    Am I close with my guess?
    Each day is a learning experience.

  11. #11
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,907
    Mentioned
    139 Post(s)
    Tagged
    2 Thread(s)
    You want print_r to return the data, not echo it (your echo statement already does that), so you need to put true as the second parameter:

    PHP Code:
    echo '<pre>'print_r($rowtrue), '</pre>'
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  12. #12
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    While putting this in various places in my code makes the page look strange, it does seem to be helping me understand what is happening and ...

    I think I am close to finding out why my script is not working.
    Each day is a learning experience.


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
  •