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 Quote Link to comment 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); ?> Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.