twice data output using foreach!?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gphp
Forum Commoner
Posts: 29
Joined: Sun Feb 17, 2008 1:40 pm

twice data output using foreach!?

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: twice data output using foreach!?

Post 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>';
(#10850)
gphp
Forum Commoner
Posts: 29
Joined: Sun Feb 17, 2008 1:40 pm

Re: twice data output using foreach!?

Post 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);
 
gphp
Forum Commoner
Posts: 29
Joined: Sun Feb 17, 2008 1:40 pm

Re: twice data output using foreach!?

Post by gphp »

RESOLVED

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

No need for "foreach"!

Thanks
Post Reply