Forum Moderators: coopster

Message Too Old, No Replies

retrieve data from two database table for combobox

         

Why_not

9:41 pm on Feb 7, 2011 (gmt 0)

10+ Year Member



I retrieve data from two database table for combobox.

<form>
<select>
<?PHP $query=mysql_query("select option.*,user.* from user,option where user.data='$id' and option.userid != user.id");
while($row= mysql_fetch_assoc($query)){ ?>
<option value=<?PHP echo $row["id"];?>><?php echo $row["name"];?></option>
<?PHP } ?>
</select></form>

Now this is retrieves :
[imgplace.com...]
Show two result for each id ! what's my problem ?

Matthew1980

11:05 pm on Feb 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you tried this query in the phpmyadmin/mysql query browser to see what you get (echo the query to screen and copy & paste into your client) from there you can debug your issue.

Hopefully, this excerpt is just a tester, else the <form> isn't coded right, and wouldn't function, but I guess as your after populating the option list first..

Personally I would do this:-

<form>
<select>
<?php
$sql = "SELECT `option`.*, `user`.* FROM `user`,`option` WHERE `user`.`data` = $id AND `option`.`userid` != `user`.`id` "
$query = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_assoc($query)){
?>
<option value="<?PHP echo $row["id"];?>" ><?php echo $row["name"]; ?></option>
<?PHP
}
?>
</select></form>

You get the idea there. If $id is numerical quoting it isn't advised (cue rocknbil for me getting it backwards again ;))

On second read through, this >> "SELECT `option`.*, `user`.* doesn't seem right to me, though I am not a sql expert, but it doesn't look right to me, lets just see what happens when you echo the sql to screen & put that into your client to see the result from command line..

Cheers,
MRb