database table readout in a loop

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

Not exactly sure what you’re trying to achieve, but what I think you’re looking for is mysql_fetch_assoc().

sorry, for explaining it baldy.
the link you send is not bad, but i wonder if i’d manage to change it to work like i want.

LOOK, THIS IS THE DOCUMENT, WHERE I, IN THE BEGINNING, BUILD AN ARRAY WITH THE FIELDS AND VALUES OF THE CURRENT TABLE
(there are docens of tables, so i want to minimize the amout the work to write down all the fields and values all the time):

$db = array(‘spendenzaehler’ => $_POST[‘spendenzaehler’],
‘spendenverwendung’ => $_POST[‘spendenverwendung’]);

// case 1 - the user submitted a new record for insertion
if (($_POST[‘submitted’]) && (!$_GET[‘update_record’])) {
insertRecord($db);
}

// case 2 - the user submitted an existing records ID to fill the form with the values to be edited
elseif ((!$_POST[‘submitted’]) && ($_GET[‘update_record’])) {
getRecord($db);
}

// case 3 - the user submitted an already existing record for actualisation
elseif (($_POST[‘submitted’]) && ($_GET[‘update_record’])) {
updateRecord($db);
}

AND HERE, THE FUNCTIONS:

function insertRecord($db) {
foreach($db as $field => $value) {
$fields .= $field.",";
$values .= “’”.$value."’,";
}

$fields = substr($fields,0,strlen($fields)-1);
$values = substr($values,0,strlen($values)-1);

$sql = "INSERT INTO $GLOBALS[table] ($fields) VALUES ($values)";

$success = mysql_query($sql, $GLOBALS[connectionID]);

}

function updateRecord($db) {
$sql = "UPDATE $GLOBALS[table] SET ";

foreach($db as $field => $value) {
	$sql .= "$field = '$value',";
}

$sql = substr($sql,0,strlen($sql)-1);

$sql .= " WHERE id = '$GLOBALS[id]' ";

$success = mysql_query($sql, $GLOBALS[connectionID]);

}

function getRecord($db) {
$sql = “SELECT * FROM $GLOBALS[table] WHERE id = $GLOBALS[id]”;

$result = mysql_query($sql, $GLOBALS[connectionID])
or die ("Auswahl aus der Datenbank nicht m?glich.");

// THIS IS, HOW IT IS NORMALLY DONE

$spendenzaehler = mysql_result($result, 0, "spendenzaehler");
$spendenverwendung = mysql_result($result, 0, "spendenverwendung");

 // AND THIS IS, HOW I - SOMEHOW - LIKE TO HAVE IT.

foreach($db as $field => $value) {
            $sql = '$result, 0, "'.$field.'"';
	$value = mysql_result($sql);
}

}

thank you very very much, cheers andre

Sponsor our Newsletter | Privacy Policy | Terms of Service