SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to print querry results stored in php array???

    as the title say, I stored results of mysql query in an PHP array and I need a way to print or access them from that array? this is what I have

    <?php
    // Connecting, selecting database
    $link = mysql_connect('localhost', 'root', 'regdump')
    or die('Could not connect: ' . mysql_error());
    //echo 'Connected successfully \n';
    mysql_select_db('test_results') or die('Could not select database');

    // Performing SQL query
    $query = 'SELECT name, phone, zip, country from newTable;
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());

    while($output = mysql_fetch_array($result)){
    $result_array[] = $output;
    }
    print ($result_array['tc_data']);
    ?>


    The page prints nothing. I have tried everything here. so appreciate any help in this regard. I was to be able to access all columns in that querry results. OH,Yes there is data in the table.

    Thank you

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can either print the entire array using print_r.

    Alternatively, you can step through the array record by record using foreach and print the part you need.

    If you're looking to access a single varialble in the array, you can reference it using:

    Code:
    print $result_array[0]['phone']; //prints the phone number of the first record in the result

  3. #3
    SitePoint Zealot CalebMingle's Avatar
    Join Date
    Jul 2007
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to see the structure of the array, add this to the bottom of the file:

    PHP Code:
    echo '<pre>';
    print_r($result_array); 
    Then you can see how your array is setup.

    If you want to access all the records, you already have a while(), so you could access them that way..

    Adding to the $result_array just increments it for each record, so the first record would be: $result_array[0], the next one would be $result_array[1], and so on.

    Then after those increments [0], [1], etc.. you would be able to access the information from the database (fields, etc).

    So to get the field 'tc_data' from the first row.. just do this:

    PHP Code:
    echo $result_array[0]['tc_data']; 
    Or to get them from all the rows..

    PHP Code:
    foreach ($result_array as $row) {
        echo 
    $row['tc_data'] . '<br />';

    I'm not sure why you would want to do that though, since you've already got a while up there, anyways.
    :: Caleb Mingle (@dentafrice)
    :: PHP Developer.
    :: g+: http://gplus.to/mingle

  4. #4
    SitePoint Enthusiast jagat_21's Avatar
    Join Date
    Jun 2007
    Location
    Ahmedabad,India
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Small Function For Debugging Purpose ::

    function _dump($object, $stop = '')
    {
    echo "<pre>";
    var_dump($object);
    echo "</pre>";
    if ($stop == 1)
    {
    exit();
    }
    }

  5. #5
    SitePoint Enthusiast Boxer's Avatar
    Join Date
    Jan 2008
    Location
    Denver, Colorado
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why you're placing results to the array at all? That doesn't seem to make sense. Just output it right away:

    PHP Code:
    <?php
    // Connecting, selecting database
    $link mysql_connect('localhost''root''regdump')
    or die(
    'Could not connect: ' mysql_error());
    //echo 'Connected successfully \n';
    mysql_select_db('test_results') or die('Could not select database');

    // Performing SQL query
    $query 'SELECT name, phone, zip, country from newTable';
    $result mysql_query($query) or die('Query failed: ' mysql_error());
    ?>
        
    <table>
        <?while($output mysql_fetch_array($result)):?>
            <tr>
                <td>Name: <?=$output['name']?></td>
                <td>Phone: <?=$output['phone']?></td>
                <td>Zip: <?=$output['zip']?></td>
                <td>Country: <?=$output['country']?></td>
            </tr>
        <?endwhile?>
    </table>


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
  •