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?
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:
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>"; }
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):
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:
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
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):
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?
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.....also similar to Rainulf so it would appear. im confussed, surely all three of a our solutions would do it?