Page 1 of 1

twice data output using foreach!?

Posted: Sun Feb 17, 2008 1:43 pm
by gphp
I am loading data from database but I get the data twice!?

***** PLEASE USE

Code: Select all

 WHEN POSTING *****[/color]

[syntax=php]if (is_array($fetch)){
 
    foreach ($fetch as $data){
 
        print "<table><tr><td>".$data."</td></tr></table>";
    }
 
}[/syntax]

Display:
string1

string1

string2

string2

string3

string3

Why it would do that?
Thanks.

Re: twice data output using foreach!?

Posted: Sun Feb 17, 2008 2:05 pm
by Christopher
What does the code where you load $fetch look like? Do this to see what is in fetch:

Code: Select all

echo '<pre>' . print_r($fetch, 1) . '</pre>';

Re: twice data output using foreach!?

Posted: Sun Feb 17, 2008 2:16 pm
by gphp
Well, this is the ouput now!?

Code: Select all

 
Array
(
    [0] => string1
    [tablecolumn1] => string1
    [1] => string2
    [tablecolumn2] => string2
    [2] => string3
    [tablecolumn3] => string3
)
 
This is how I am accessing the db:

Code: Select all

 
$DB->sql_connect('host', 'user', '', 'dabatasename');
$query = $DB->query("SELECT * FROM tablename WHERE tablecolumn2='username'");
$fetch = $DB->fetch_row($query);
 

Re: twice data output using foreach!?

Posted: Sun Feb 17, 2008 3:01 pm
by gphp
RESOLVED

printf ("ID: %s BLOB1: %s BLOB2: %s", $fetch[0], $fetch["1"], $fetch[2]);

No need for "foreach"!

Thanks