andrecito Posted February 14, 2008 Share Posted February 14, 2008 dear php-pros, is there any idea on how to read a database-table into an array? lets say, i know the table's and the array's structure. the tables fields have the same names as the arrays names. now, i just want to pass the array to a function, fill it with the values of the table and be able to work with them. i.e. array definition - global: $db = array('aktuelles_title' => $aktuelles_title, 'aktuelles_text' => $aktuelles_text); and, within the function: $sql = "SELECT * FROM $table WHERE id = $id"; $result = mysql_query($sql, $connectionID); plus something like this (which of course doesnt work): foreach($db as $field => $value) { $sql = '$result, 0, "'.$field.'"'; $value = mysql_result($sql); } i very much apreciate any help or code suggestions. thanks in advance, andre Link to comment https://forums.phpfreaks.com/topic/91057-database-table-readout-in-a-loop/ Share on other sites More sharing options...
micah1701 Posted February 14, 2008 Share Posted February 14, 2008 <?php $value = array(); foreach($db as $field => $value) { $sql = '$result, 0, "'.$field.'"'; $value[] = mysql_result($sql); } print_r($value); ?> Link to comment https://forums.phpfreaks.com/topic/91057-database-table-readout-in-a-loop/#findComment-466748 Share on other sites More sharing options...
andrecito Posted February 14, 2008 Author Share Posted February 14, 2008 dear micah, your proposal doesnt work... wonder if i explained it badly or do not understand to change your code somehow. the idea is, that there is a globally defined array like this: $db = array('field1' => $value1, 'field2' => $value2); and then, there is a function like the following: function getRecord($db) { $sql = "SELECT * FROM $GLOBALS[table][tr][td] WHERE id = $GLOBALS[id]"; $result = mysql_query($sql, $GLOBALS[connectionID]); foreach($db as $field => $value) { $sql = '$result, 0, "'.$field.'"'; $value = mysql_result($sql); } } after the call of the function i would like the global array to contain the content of the database table. thanks a lot, andre Link to comment https://forums.phpfreaks.com/topic/91057-database-table-readout-in-a-loop/#findComment-466804 Share on other sites More sharing options...
mem0ri Posted February 14, 2008 Share Posted February 14, 2008 You're missing the final step of taking the $result-set object and fetching each row from it as an array...try something like: $result = mysql_query($sql, $GLOBALS[connectionID]); while ($row = mysql_fetch_array($result) $db[] = $row; Your $db variable will then look like: $db = array(0=>array('field1'=>$value1...), 1=>array('field1'=>$value2)...); ...where each array-value in $db is an array that contains an entire row of the database. Link to comment https://forums.phpfreaks.com/topic/91057-database-table-readout-in-a-loop/#findComment-466819 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.