...

View Full Version : Best way to extract database array



funnymoney
05-12-2008, 02:40 PM
I need to find way to extract an array of users from database, but extract() function doesn't work like i thought it will

I'm using simple way to loop through database, but can't find simple way to return that array so i can use it later.

Code:


public function statistike() {

$sql = ("SELECT * FROM korisnici");
$result = mysql_query($sql);

while ($podaci = mysql_fetch_assoc($result)) {
echo $podaci["id"]. " ";
echo $podaci["ime"]. " ";
echo $podaci["email"]."<br>";

extract($podaci);
}

mysql_free_result($result);

print_r($ime);

return $podaci;
}


i read PHP manual, and function extract should give me arrays $ime ($name) $id and $email, but it doesnt work.

for now i'm using array_push, but i thought it would be possible to get array of names by using extract, but looks like it doesn't work

Stooshie
05-12-2008, 02:57 PM
You are doing the extract within the while loop, so it is being over-written each iteration.

Do the print_r($ime) inside the while loop.

Actually $ime won't be an array so you will only need to echo it.

logictrap
05-12-2008, 03:17 PM
Try something like this:

public function statistike() {

$sql = ("SELECT * FROM korisnici");
$result = mysql_query($sql);

while ($podaci = mysql_fetch_assoc($result)) {
$myarray()= $podaci;


}

mysql_free_result($result);

print_r($myarray);

return $myarray;
}

Stooshie
05-12-2008, 04:47 PM
funnymoney, Is there any particular reason you need them as separate variables?

Logictrap is right in that the user details will be easier to handle as a two dimensional array.

"extract()" just turns an array into a set of vars. (eg $myarray["mykey"] = "myvalue" just becomes $mykey = "myvalue").

funnymoney
05-12-2008, 06:08 PM
i'm using .inc file to populate it with variables using pregmatch.

.inc file is something like this


<p>Ime: {%ime%} Email: {%email%}

now from database i get variables to populate my inc file. my $ime array with names lookes like this for now


$podaci['ime'] = array ("duje", "pave", "mate", "sime");

print_red it lookes like this

array ([ime] =>
[0] -> 'duje'
[1] -> 'pave')
etc...

but then i use pregmatch in (for now) eg. foreach loop to replace every token (%ime% and %email%) with user info, to get something like


<p>Ime: Duje E-Mail: duje@duje</p>
<p>Ime: Pave E-Mail: pave@pave</p>
etc, in that order

my pregmatch is this



foreach ($podaci['ime'] as $key => $val) {
preg_replace ('/{%ime%}/', $val, $html_inc_file);
}


it tourns out to be a quite dificult to work with array that has structure like that, and i would also like not to use array_keys or array_values so that it goes as fast as possible..

hope you understand. i would give you more info, but i'm on remote computer and i don't have direct access to scrip, but tomorow i could write more data :)

funnymoney
05-12-2008, 06:11 PM
"extract()" just turns an array into a set of vars. (eg $myarray["mykey"] = "myvalue" just becomes $mykey = "myvalue").

yes i know, but it looks like i don't know how to use it right. maybe i didn't put to much effort couse i have sooo much other stuff i need to tend to :busybusy:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum