Jump to content

[SOLVED] output of second column in select


nomis

Recommended Posts

Okay, this one is confusing me...it may be difficult, or I'm not sure how to find a solution

 

I have a form which for a select, it grabs an id from a separate table, and then prints the id name as part of the <select> option.

 

This is the code:

 

	
<?php
$r = mysqli_query($dbc, 'select * from sponsor_type order by sponsor_type');
while ($row = mysqli_fetch_array($r)) {
echo "<option value =\"$row[0]\">$row[1] </option> \n";
}		?></select></td> 
</tr>
<tr>

 

This effectively produces the name of the sponsor_type, but grabs the primary key, which is sponsor_type_code.

 

I want to produce a confirmation which states which type of sponsor was selected.  Right now, I'm only able to grab the sponsor_type_code, and not the sponsor.  This (of course), would be meaningless to the user.

 

I have defined the variable for sponsor_type_code (as it essential for the next step... not getting into that here, that may be a different issue)

  $sponsor_type_code =  $_POST['sponsor_type_code'];

and am calling it with this:

echo 'Sponsor Type Code: ' . $sponsor_type_code . '<br />';

This works fine, providing an answer of 'LO'  (the code for "Lodging") in one instance, but the problem is that I want to be able to bring back sponsor_type (which is one column to the right of sponsor_type_code) and I'm not sure of the correct syntax...

 

I've tried a few things.  First of all I tried this:

$st = "Select * from sponsor_type where 'sponsor_type_code' = '$sponsor_type_code'";
$resultst = mysqli_query ($dbc, $st)
	or die ('Error: '.mysqli_error($dbc));

echo 'Sponsor Type: ' . $st . '<br />';

which of course just brought back this result

 

Sponsor Type: Select * from sponsor_type where 'sponsor_type_code' = 'LO'

 

I can see that I am able to get some close data... is there a way for me to just bring back the second column from this query?  I tried naming a second variable $sponsor_type, but of course, it's not really a post variable... do I link it to $st somehow?  something like the below (which of course does not work... I know the syntax is wrong...

$sponsor_type = $st.col[1];

 

thanks in advance for any ideas

 

 

 

I re-read your post like 4 time and am still not sure what you're asking.

 

are you asking how to get the "sponsor type" from your table by passing it the sponsor type code?

 

if so, in this line:

$st = "Select * from sponsor_type where 'sponsor_type_code' = '$sponsor_type_code'";

 

just select the specific column (presumably named sponsor_type) instead of selecting *

 

$st = "Select sponsor_type from sponsor_type where 'sponsor_type_code' = '$sponsor_type_code'";

 

 

if not, could you maybe try to re-explain what the issue is?

Thanks... I had actually already tried that but  sponsor_type gets treated as part of the text string.

 

I had a head-slap moment and figured out what I was doing wrong... I left out the result rows in the query, so of course that variable $st was just spitting back the contents of the query.

 

Here's what I ended up doing which worked perfectly:

 

$st = "Select sponsor_type from sponsor_type where sponsor_type_code = $sponsor_type_code'	";
$resultst = mysqli_query ($dbc, $st)
	or die ('Error: '.mysqli_error($dbc));
$row = mysqli_fetch_array($resultst);

echo 'Sponsor Type: ' . $row[0]. '<br />';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.