1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

mySQL Array?

Discussion in 'PHP' started by gerard0986, Jul 14, 2010.

  1. #1
    Not sure if this is the right place, but it should since it doesn't do too much with mySQL, but more on the PHP.

    I want to get all the info from a mySQL database and use the info in an array.

    $stuff=array
     (
    
        array
        (
        "Stuff from database here.",
        "And here."
        ),
    
        array
        (
        "More stuff from database here.",
        "And more here."
        ),
    
    );
    Code (markup):
    Etc. It would repeat the arrays for all the mySQL info.

    Now how would I do that?
     
    gerard0986, Jul 14, 2010 IP
  2. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Do you want the entire database (and all tables within it) or just a specific table?
     
    Deacalion, Jul 14, 2010 IP
  3. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #3
    I believe it would be something like this:
    
    $result = mysqli_query($link, "SELECT watever, watever2 FROM databse"); // $link is your connection to the database
    while ($row = mysqli_fetch_array($result)) { // loop will exit at the end of row
       $the_array[] = array('watever'   => $row['watever'], // $row is a row from your table, each loop will go to each row
                            'watever2' => $row['watever2'] );
    }
    PHP:
     
    Last edited: Jul 14, 2010
    Rainulf, Jul 14, 2010 IP
  4. gerard0986

    gerard0986 Peon

    Messages:
    53
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    A specific table.
     
    gerard0986, Jul 14, 2010 IP
  5. kaviarasankk

    kaviarasankk Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    to display whole database
    whats the code
     
    kaviarasankk, Jul 14, 2010 IP
  6. kaviarasankk

    kaviarasankk Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    to display specific table

    $query = mysql_query("SELECT * FROM TABLENAME");
    echo "<table>";
    while($fetch = mysql_fetch_array($query)){

    echo "<tr>";
    echo "<td>".$fetch[]."</td>";
    echo "</td>";

    }
     
    kaviarasankk, Jul 14, 2010 IP
  7. gerard0986

    gerard0986 Peon

    Messages:
    53
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yeah, but I want it to be in this format:

    $stuff=array
     (
    
        array
        (
        "Stuff from database here.",
        "And here."
        ),
    
        array
        (
        "More stuff from database here.",
        "And more here."
        ),
    
    );
    Code (markup):
     
    gerard0986, Jul 14, 2010 IP
  8. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #8
    This will do that. The first element of $tableArray is actually another array that contains the tables fieldnames.

    
    <?php
        $tableName = 'some_table'; // change to your table
        $tableArray = array();     // the array that will hold everything
    
        $query = mysql_query('SELECT * FROM ' . $tableName);
    
        $columns = mysql_num_fields($query); 
        for($i = 0; $i < $columns; $i++) $tableArray[0][$i] = mysql_field_name($query, $i);
        while($row = mysql_fetch_row($query)) $tableArray[] = $row;
                                                                                                     
        // $tableArray now holds the table in array form, to test - uncomment the next line:
        // print_r($tableArray);
    ?>
    
    PHP:
     
    Deacalion, Jul 14, 2010 IP
  9. kaviarasankk

    kaviarasankk Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Smart
    but he/she is asking an array inside that two array in which 1st array must have few data and 2nd one must have lot
     
    kaviarasankk, Jul 14, 2010 IP
  10. gerard0986

    gerard0986 Peon

    Messages:
    53
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Yeah, I have one array which is ok, but getting the mySQL info into another array inside the first one is the hard part.

    Also, in 1 specific table I need to grab 2 specific things and for each "row" put it in this format:

    array
        (
        "Stuff 1 from Row 1",
        "Stuff 2 from Row 2"
        ),
    
        array
        (
        "Stuff 1 from Row 2",
        "Stuff 2 from Row 2"
        ),
    
    Code (markup):
     
    gerard0986, Jul 14, 2010 IP
  11. dbsuk

    dbsuk Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Something like this maybe:

    
    
    // create empty $array as array();
    $array = array();
    
    // the mysql query query
    $sql = mysql_query("SELECT field1, field2 FROM mytable");
    
    // loop through the results of the mysql_query
    while($row = mysql_fetch_array($sql)) {
        
         // add the row data to the $array[]
         // note the use of [] at the end of $array[]
         // the use of [] adds the array of db row data into the $array
         // without the [] each loop would overwrite the previous array with the row data array.
         $array[] = array(  $row['field1'],
                            $row['field2']   );
    
    } // end while loop
    
    // out the array
    print_r($array);
    
    // assuming there were two rows of data in "mytable" print_r($array); would out
    
    Array
    (
        [0] => Array
            (
                [0] => field1 from row 1
                [1] => field2 from row 1
            )
        [1] => Array
            (
                [0] => field1 from row 2
                [1] => field2 from row 2
            )
    )
    
    
    PHP:
    Is this what you mean?
     
    dbsuk, Jul 15, 2010 IP
  12. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #12
    umm... dbsuk, code does almost the same thing as mine. If this table is normalized then the fields shouldn't contain more than one type of data or an array.
    Any chance I could see the table schema? then I could code something for it.
     
    Deacalion, Jul 15, 2010 IP
  13. dbsuk

    dbsuk Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Deacalion.....also similar to Rainulf so it would appear.

    im confussed, surely all three of a our solutions would do it?
     
    dbsuk, Jul 15, 2010 IP