Jump to content

[SOLVED] output specific data dependent on two id #'s from separate tables


vynsane

Recommended Posts

okay, so i really wouldn't know how to search for an answer to this one - i've been working with php/mysql for a few months now and have usually been able to figure stuff out on my own, but this one is baffling me. i'm pretty sure that there's a more elegant and cleaner way to get the data that i'm trying to pull from the DB, but i thought this would work.

i have a username stored in a cookie through a login form. this username has a corresponding id # in the "Members" table. that id # corresponds to the "Members_id" column in another table, "NewsItem".

i'm trying to output only the information from NewsItem that pertains to that username (through comparing the id# in "Members" to the Members_id# in "NewsItem"). what i've been trying is below:

[code]
$username = $_COOKIE['----name of cookie----'];
        $userid = mysql_query("SELECT id FROM Members WHERE username = '".$username."'");
            while ($Members_id = mysql_fetch_array($userid)){
            $articles = mysql_query("SELECT id, title FROM NewsItem WHERE Members_id = '".$Members_id."'");
              while ($row = mysql_fetch_array($articles)) {
                  echo '<li>'.$row["title"].'<br />';
                  echo '<a href="preview.php?id='.$row["id"].'" target="blank">View</a>&nbsp; &nbsp;<a href="editarticle.php?id='.$row["id"].'">Edit</a></li>';
              }
        }
[/code]

when i add

[code]
print_r($Members_id);
[/code]

before $articles it outputs the correct id number, but i can't get it to compare it to the Members_id in NewsItem. when i artificially change

[code]
Members_id = '".$Members_id."'");
[/code]

to

[code]
Members_id = '1'");
[/code]

i get the correct articles from the database in comparison to the Members.id and NewsItem.Members_id. obviously i've made this over-complicated, or i'm missing some little stupid thing, but at my level of experience this seemed like the answer. please help?
Link to comment
Share on other sites

thanks for your quick response...

$Members_id is an array --->

[code]$Members_id = mysql_fetch_array($userid)[/code]

if it need not be an array, i don't know how to get the value... it probably doesn't need to be an array, seeing as how i'm only getting a single id value...

anyway, i'm just using
[code]
print_r($Members_id);
[/code]

to see if it's getting the correct value (sort of a test)... and it is, the page outputs:
[code]
Array ( [0] => 1 [id] => 1 )
[/code]

but when i change it to

[code]
print_r($members_id);
[/code]

i get nothing. basically, i'm testing to see where my script is breaking, and evidence is that it's NOT breaking before the "$articles" query. which means it must be the "$articles" query which IS breaking... and it's been driving me nuts because i don't see a reason why this shouldn't work. but obviously it doesn't and i can't figure it out.
Link to comment
Share on other sites

I was copy and pasting code you had picked out.
Change
$articles = mysql_query("SELECT id, title FROM NewsItem WHERE Members_id = '".$Members_id."'");

To
$articles = mysql_query("SELECT id, title FROM NewsItem WHERE Members_id IN ('".implode(',', $Members_id)."')");
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.