vynsane Posted January 29, 2007 Share Posted January 29, 2007 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> <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? Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 29, 2007 Share Posted January 29, 2007 print_r() is for printing arrays. Is $Members_id an array? What happens if you do print $members_id instead? What does it print out? Quote Link to comment Share on other sites More sharing options...
vynsane Posted January 29, 2007 Author Share Posted January 29, 2007 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 29, 2007 Share Posted January 29, 2007 Members_id = '".$Members_id."'");If $Members_id is an array, this will showMembers_id = 'Array");You need to doMembers_id IN ('".implode(', ', $Members_id)."')"); Quote Link to comment Share on other sites More sharing options...
vynsane Posted January 29, 2007 Author Share Posted January 29, 2007 ummm... i'm confused as to where to put that code above in my code... Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 29, 2007 Share Posted January 29, 2007 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)."')"); Quote Link to comment Share on other sites More sharing options...
vynsane Posted January 29, 2007 Author Share Posted January 29, 2007 ah, i see... sorry, didn't know if it was something extra to tack on... edited to your specs and it works! thanks so much! (i definitely wouldn't have figured that out on my own!) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.