PDA

View Full Version : Foreach() problem


matthew tucker
03-06-2003, 11:35 PM
In admin/user.php I have inserted the following code to extract data from a custom table "memberinfo" and display using the makeinputcode() function of vbulletin.

maketableheader("Memberinfo details TEST (code in: admin/user.php line 308)");

$myquery="SELECT * FROM memberinfo WHERE email='$user[email]'";
$myresult=$DB_site->query_first($myquery);

foreach ($myresult as $mykey=>$pointer){

makeinputcode($mykey,$mykey,$myresult[$mykey]);

}

Problem is I get two rows for each record. What have I done wrong??

MUG
03-06-2003, 11:50 PM
It's the way mysql_fetch_array() works - it returns both a numerical and associative index for each column unless specified otherwise. You can change that block of code to:maketableheader("Memberinfo details TEST (code in: admin/user.php line 308)");
$myquery=$DB_site->query("SELECT * FROM memberinfo WHERE email='$user[email]'");
$myresult = mysql_fetch_array($myquery, MYSQL_NUM);

mysql_free_result($myquery);

foreach ($myresult as $mykey=>$pointer){
makeinputcode($mykey,$mykey,$myresult[$mykey]);
}
which should work.

Sebastian
03-06-2003, 11:52 PM
add LIMIT 1 to the query.

MUG
03-06-2003, 11:53 PM
Originally posted by Sebastian
add LIMIT 1 to the query. What? That wouldn't make any difference. It's only fetching one row in the first place.

Sebastian
03-06-2003, 11:55 PM
its adding more results, and he/she only want 1,. not 2,3,4,5...
or did i read the post wrong?

MUG
03-06-2003, 11:57 PM
Originally posted by Sebastian
its adding more results, and he/she only want 1,. not 2,3,4,5...
or did i read the post wrong? He's using foreach() on something returned by mysql_fetch_array() - which by default includes each column twice, unless you specify otherwise.

matthew tucker
03-07-2003, 01:11 AM
I (he!) want to step through all the fields in the array!

I started to use extract() then hard-code all the makeinputcode bits, then I though no, why not build an interface to the memberinfo table on the fly rather than hard code it.

Foreach() looked a bettersolution than while() ...

I'll try your suggestions.

I was trying to use the vB functions but now I think I'd probably be better off just using plain PHP and mySQL so I understand the code better, no? I'm a bit new to PHP, trying hard ...

Matthew

matthew tucker
03-07-2003, 01:16 AM
Nope

I get

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /[path]/user.php on line 314

matthew tucker
03-07-2003, 01:45 AM
All fixed. thanks. I needed the MYSQL_ASSOC value!

Works now, thanks x 10^6

Matthew