Jump to content

database table readout in a loop


andrecito

Recommended Posts

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

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.