Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 9 of 9

Thread: PHP Help

  1. #1
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    Ok...

    So this: $row['name']; will output the first name in my mySQL query where $row = mysql_fetch_array($query);

    This: $row['name'][1]; will output the first letter of the first name in my mySQL query.

    But what do I do for it to output the second name in my mySQL query?
    JR

  2.  

  3. #2
    WDF Staff Wired's Avatar
    Join Date
    Apr 2003
    Posts
    7,656
    Member #
    1234
    Liked
    137 times
    2nd Name or 2nd letter? More specifics man...
    The Rules
    Was another WDF member's post helpful? Click the like button below the post.

    Admin at houseofhelp.com

  4. #3
    Senior Member Brak's Avatar
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    3,413
    Member #
    1217
    Liked
    2 times
    Do something like this...

    Code:
    $result = mysql_query($sql);
    while ($row = mysql_fetch_array($result){
      $i++;
       echo "This is row $i . Name: ". $row['name'];
    }
    Yes.. that's one equals sign. It will not be a valid statement once it goes through the last row.
    Kyle Neath: Rockstar extraordinare
    The blog | The poetry site | The Spore site

  5. #4
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Maybe I've misunderstood, but I guess second name in this context doesn't mean "lastname" (if so, just add another echo statement).

    If multiple firstnames, iterate through the rows with a "for" loop.

    (BTW $row['name'][1], gives the second and not the first letter of the stored name, $row['name'][0] gives the first)
    S. Rosland

  6. #5
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    Originally posted by JR
    Ok...

    So this: $row['name']; will output the first name in my mySQL query where $row = mysql_fetch_array($query);

    This: $row['name'][1]; will output the first letter of the first name in my mySQL query.

    But what do I do for it to output the second name in my mySQL query?
    Technically, that should be:
    PHP Code:
    $row['name']{2
    i.e., use braces to access a character index.

    If you have a matrix of strings and you want to get, say, the third character in row 8 column 12, use:
    PHP Code:
    echo $matrix[7][11]{2}; 
    ...IIRC.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  7. #6
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    No, I meant the second name (record 2), not surname.
    Code:
    ID  Name
    1   Fred
    5   Bill
    9   Bob
    So is there an easy way to get Bill through $row['name']something?

    Brak: A while statement is not acceptable, because I want to use them as seperate variables on the page.
    JR

  8. #7
    Senior Member nsr81's Avatar
    Join Date
    Oct 2002
    Posts
    1,132
    Member #
    250
    Liked
    15 times
    PHP Code:
    $result mysql_query($sql);

    $i 1;
    while (
    $row mysql_fetch_array($result))
    {
       ${
    "name$i"} = $row['name'];
       
    $i++;

    That will get your names in variables like $name1, $name2, and so on. And they will be available for use later on. Not sure how usefull this will be though.

    If you could elaborate on what you are trying to do, perhaps someone can come up with a better solution.
    There and Back Again :Ogre:

  9. #8
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Hi JR.

    You can use a while loop to (instead of echoing output)create a simple array containing all names from the query.

    After running this, all names will be stored in the $var array.
    You can obviously easily further assign all array elements into individual variables afterwards.
    PHP Code:
    while ($row mysql_fetch_array($result))
    {  
      @
    $i++; 
       
    $var[$i]=$row['name'];

    Hope that is what you looked for.

    EDIT:
    nsr81 and myself seem to have posted the same answer at exactly the same time!
    S. Rosland

  10. #9
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    Ok thanks.

    Filburt: Please give $5 from my account to rosland, I'll give the other $5 to nsr.
    JR


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 11:43 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com