Forum Moderators: coopster

Message Too Old, No Replies

grab mysql field name with php script

         

makimoto

9:36 pm on Apr 15, 2005 (gmt 0)

10+ Year Member



hi,
i have a php script that is finding a row in a mysql db and returning one of the field values from that row, and assigning the value to a variable. this is all working well, pretty basic. but i have an requirement that is a bit weird. i need to also grab the field name (aka the column title) for the value that i find.

is this possible? if so, how? please advise. TIA,

jatar_k

9:49 pm on Apr 15, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



[php.net...]
[php.net...]

makimoto

4:17 pm on Apr 19, 2005 (gmt 0)

10+ Year Member



thanks jatar,
the fetch function is more info than i need, so the mysql_field_name() function seems like it will do the trick. but i am having a hard time getting it to work based on the instructions given on php.net. i have established what the resource id is for the data i want to get mysql_field_name()for. it is id# 4. i guess my problem is that id#4 is an html <select><option> drop down menu that is populated with several different field values from a single row the mysql db using a an if/while loop. i want to grab the field name of the value the user chooses from the drop down menu. this is how i am unsuccessfully trying to do it now on line 97:

$type2= mysql_field_name(4, 0);

the error i get is: Warning: mysql_field_name(): supplied argument is not a valid MySQL result resource in /nfs/2/makimoto/mysite.com/htdocs/app/showitemCAP.php on line 97

i also do not understand field offset, so i left it at zero. but i really don't know what i am doing here. i can supply surrounding code if required. any thoughts much appreciated!

thx,
makimoto

jatar_k

5:03 pm on Apr 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I am thinking that the problem is more logic based

if you are constructing the drop down why not add a little mor e info so you already know the fieldname?

makimoto

5:26 pm on Apr 19, 2005 (gmt 0)

10+ Year Member



the html drop down itself does have extra info added to it for the sake of the user. but when the form is submitted it still only submits the values that were originally grabbed from the mysql table.

but now that you mention it, perhaps extra string info can be added to the field data when the while statement originally pulls the data. i tried to do this, but i can't figure out how to append the string i want. for example, here is the first iteration of the while statement:

<select name=\"type\">";
while ($types = mysql_fetch_array($get_types_res)) {
$item_I_CT = $types['I_CT'];

and the first iteration of the corresponding option tag:

$display_block .= "<option value=\"$item_I_CT\">Cereb Tack \$$item_I_CT</option>

when i submit this form, it only submits the value from the 'I_CT' field in the DB, which is the price of the Cereb Tack (btw). so how would i append "Cereb Tack" to the value from the DB?

thx again.

makimoto

5:39 pm on Apr 19, 2005 (gmt 0)

10+ Year Member



thanks for that bit of "logic" there jatar. i was able to apend it to:

$item_I_CT = $types['I_CT'];

by simply going:

$item_I_CT = $types['I_CT']." Cereb Tack";

thanks again!

mak

jatar_k

6:25 pm on Apr 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



nice to come back and see you sorted it :)