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
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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.