SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Return rows as array?

    How can I take query the rows of a database and have them return as an array?

    Thanks,
    e39m5

  2. #2
    Pandora Tipem's Avatar
    Join Date
    Feb 2006
    Posts
    450
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a little sample... remember, the PHP manual is your friend.

    PHP Code:
    $connection mysql_connect("localhost""blah""blah");
    mysql_select_db("yourfancydatabase");

    $query mysql_query("select * from myFancyTable");
    while (
    $resultsrow mysql_fetch_array($query))
    {
        echo 
    "Name: ".$resultsrow['name'];
        echo 
    "Address: ".$resultsrow['address1234'];

    The variable $resultsrow is your results array. You then can access values in this array based on your column names in your DB table (e.g., we're displaying the 'name' column and 'address1234' column. Hopefully you know a *few* things about PHP so you can change the values accordingly.
    Tipem no fancy sig, yup
    Need Neopets Help? Try Neopets
    fun adoptable eggs

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok I think im just not sure how to access it correctly.

    if I say
    PHP Code:
    $resultsrow mysql_fetch_array($query)
    $nameResults $resultsrows['name']; 
    will I be able to access my $nameResults variables through $nameResults[0], $nameResults[1], $nameResults[2], etc? Or do I have to set it up with a while loop? Sorry, I've never had to do this before so that may be completely off.

    Thanks,
    e39m5

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any ideas? I just want to get my sql results into a numbered array so my variables are $results[0], $results[1], $results[2], etc. I feel like its really easy, just don't know how to do it or what to search.

    Thanks,
    e39m5

  5. #5
    Non-Member I87's Avatar
    Join Date
    Mar 2006
    Location
    UK
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    umm well you could always do this


    PHP Code:
    mysql_connect('localhost''user''pass');
    mysql_select_db('db');

    $myArray = Array();

    $query mysql_query('SELECT row FROM table');
    while (
    $fetch mysql_fetch_assoc($query))
    {

    $myArray[] = $fetch['row'];



  6. #6
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps the following is what you need?
    PHP Code:
    while( $rows mysql_fetch_array($resultMYSQL_NUM)){
    echo 
    $rows[0], ' -- '$rows[1], '<br>';


  7. #7
    SitePoint Evangelist
    Join Date
    Jun 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am using a similar code to mudsharks to accomplish this and it is working fine, but I'm still suprised theres no simpler way to do this. I guess there aren't many uses for it, I am personally using it to send my query content through a session. At one point I was sending the variables as a string and then exploiding it, but this seems better.

    e39m5

  8. #8
    SitePoint Evangelist ldivinag's Avatar
    Join Date
    Jan 2005
    Location
    N37 33* W122 3*
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you show how you want the results to show, like when you PRINT_R the array?
    leo d.


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
  •