SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    making WHILE RESULT to a variable

    Code:
    code
    
    $testResult=mysql_query("select id, name from myTable order by id ") ; 
    
    while($row = mysql_fetch_array($testResult))
    {
    
    $myRecord="(". $row['id'].")".$row['name']." <br>";
    
    echo $myRecord;
    
    }
    
    
    result
    
    (id) name
    
    (1)  Tom 
    (2)  Mary 
    (3)  Jane 
    (4)  Jack
    The code above produces the result above.

    I like to get the same result above by using one variable named "myWholeResult".

    The code below which is one of trial for it doesn't work correctly, but I hope it shows what I want.

    Code:
    trial code
    
    $testResult=mysql_query("select id, name from myTable order by id ") ; 
    
    
    $myWholeResult = while($row = mysql_fetch_array($testResult))
    {
    
    $myRecord="(". $row['id'].")".$row['name']." <br>";
    
    echo $myRecord;
    
    };
    
    echo $myWholeResult;
    
    target result
    
    (1)  Tom 
    (2)  Mary 
    (3)  Jane 
    (4)  Jack

  2. #2
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The trial code2 below can get what I want with A variable, but I guess there might be easier way for it.
    Code:
    trial code2
    
    $myWholeRecord="";
    
    $testResult=mysql_query("select id, name from myTable order by id ") ; 
    
    
    
    while($row = mysql_fetch_array($testResult))
    {
    
    $myRecord="(". $row['id'].")".$row['name']." <br>";
    
    $myWholeRecord=$myWholeRecord.$myRecord;
    }
    
    
    echo $myWholeRecord;

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Change the code inside your while block to

    PHP Code:
    while($row mysql_fetch_array($testResult))
    {
        
    $myWholeRecord .= "("$row['id'].")".$row['name']." <br>";

    .= will append data to an existing variable.

  4. #4
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much, programhis.


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
  •