开发者

Select statement with array

I have 2 tables. First has and ID and a Purpose. The other one contais rel_id and member_id. I need to go through rel_id array which matches the user_id that I have stored in session variable. Then rel_id corresponds to first tables ID and prints out Purpose. It may sound confusing but that's the way I figured out to do my task. The way I'm doing this might clear the confusion.

To get all the rel_id's that user is using I do:

$result = mysql_query("SELECT rel_id FROM $tbl_pask_rel WHERE member_id='$_SESSION[id]'")
or die(mysql_error());
$row = mysql_fetch_array($result);

To get the Purposes from the first table I use:

开发者_运维问答
$query="SELECT pask, id FROM $tbl_name WHERE '$row[rel_id]'=id";
$result = mysql_query ($query);

I want to display results in a drop down list but it only picks up THE FIRST matched Purpose and displays it. The rest arent displayed so I suppose my $row[rel_id] contains only 1 value which it picked first. I want it to go through all array and display it. Any ideas would be much appriciated! :)

Regards, Skittles


Get all the relationships and their associated data at once, you do not need or want a loop

SELECT
  rel_id,
  pask
FROM
  $tbl_pask_rel
INNER JOIN
  $tbl_name
ON
  $tbl_pask_rel.rel_id = $tbl_name.id
WHERE
  $tbl_name.member_id = '$_SESSION[id]'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜