2

so as the title says.....

here is the code I currently wrote thinking it would work and it doesnt :(

note my session userid etc is working as I can get it to print out in another field in the form so thats not the problem, but my dropbox just seems to have nothing in it. (i have created the data on the database with the user_id matching of which I am logged in with)

$userid = $_SESSION['myuserid'];
//run query to database
$query = "SELECT * FROM test_groups_tb WHERE user_id='$userid'";
mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($query))
    {
    $dd .= "<option value='{$row['group_id']}'>{$row['group_name']}</option>";
    } 

this is then used in the html:

<select name="t_group"><? echo $dd; ?></select>

can somebody help me out?

thanks

5
  • What's the HTML actually looking like after execution? Are you sure the query yields correct results vs. your expectations? Commented Mar 16, 2011 at 15:09
  • What is the output you get, any errors? How do you get $query? Commented Mar 16, 2011 at 15:10
  • edited post so you should be able to view code properly now? apologies, new to the site. Commented Mar 16, 2011 at 15:10
  • currently the form displays, just with no dropdown menu options Commented Mar 16, 2011 at 15:11
  • Next time if you ask a question, consider not typing the title in all caps. Commented Mar 16, 2011 at 15:15

3 Answers 3

3

$query is a string and therefore you cannot get any results from it. You should do something like:

$query = "SELECT * FROM test_groups_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($result))
{
    $dd .= "<option value='{$row['group_id']}'>{$row['group_name']}</option>";
} 
Sign up to request clarification or add additional context in comments.

Comments

2

It doesnt work because $query is a string. You should assign the result of mysql_query to the $query variable.

If you had error reporting on you would have seen an error like mysql_fetch_assoc expects parameter 1 to be resource, string given.

Comments

0

Personally, I always drop out of a string when i'm adding variables.. I know you don't have to do it depending on how you're set up, but my line would be:

$dd .= "<option value='".$row['group_id']."'>".$row['group_name']."</option>";

I'd also get rid of the inevitable notice by setting $dd = "" before trying to add to it, if you care about such things.

Check that your query is a resource (echo $query - expect 'resource id #nn') and that it is producing any rows [ echo mysql_num_rows($query); ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.