therocker Posted May 31, 2011 Share Posted May 31, 2011 So I'm having issue with trying to pull the ID from my database out correctly. This is the ideal way I want the ID to be pulled. http://a7.sphotos.ak.fbcdn.net/hphotos-ak-snc6/226659_219275201434540_100000561868489_827151_3402533_n.jpg But then it always pulls the ID like this and stacks the number ID ontop of each other and I want it to have separate DIVS. http://photos-e.ak.fbcdn.net/hphotos-ak-snc6/226659_219275204767873_100000561868489_827152_3422285_n.jpg This is the code that I'm using. Tell me what I'm doing wrong. <? mysql_connect("localhost", "mysql_user", "mysql_password") or die("Could not connect: " . mysql_error()); mysql_select_db("mydb"); $result = mysql_query("SELECT id, name FROM mytable"); echo "<div class=\"body_resize\"><div class=\"left\"><br /><span class='date'>$timedate</span><br /><br />"; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { printf("<h2>#%s</h2>", $row["id"], $row["name"]); } mysql_free_result($result); echo "<br /><div class=\"comment_gravatar left\"><img alt=\"\" src=\"http://photos-b.ak.fbcdn.net/hphotos-ak-snc6/229186_219276548101072_100000561868489_827154_8135363_n.jpg\" height=\"32\" width=\"32\" /></div><p>Posted By $name<br><br /><p>$message</p><br></div><div class=\"clr\"></div></div>"; if ($ip==$userip && $delcom==1) { ?> Link to comment https://forums.phpfreaks.com/topic/237952-i-need-help-pulling-an-id-from-your-database/ Share on other sites More sharing options...
seanlim Posted May 31, 2011 Share Posted May 31, 2011 You are printing the IDs in the a while loop which loops over all the results of the table, so I would expect the stacked IDs to be the output. If you intend to print only 1 ID for 1 post on the page, narrow down your mysql query to select only the relevant ID. If you intend to print 1 ID for each post on the page, include the post-printing code within the while loop as well, to ensure that on each iteration of the loop, the ID is printed once together with the post. Link to comment https://forums.phpfreaks.com/topic/237952-i-need-help-pulling-an-id-from-your-database/#findComment-1222769 Share on other sites More sharing options...
therocker Posted June 3, 2011 Author Share Posted June 3, 2011 You are printing the IDs in the a while loop which loops over all the results of the table, so I would expect the stacked IDs to be the output. If you intend to print only 1 ID for 1 post on the page, narrow down your mysql query to select only the relevant ID. If you intend to print 1 ID for each post on the page, include the post-printing code within the while loop as well, to ensure that on each iteration of the loop, the ID is printed once together with the post. And how is this possible?? If I move the ID away from the while loop, the ID will not get displayed, if I leave it in there, it will stack up and it will show errors, I need help trying to move the ID to a different part of the code that it will be able to read and display at the same time with out showing just #%s in there because I tried to move #%s outside of the while loop and I tried to move the second DIV to another part of the code and I get the same exact results, it seems that I'm not getting the answer I'm wanting for. And people may have not get what I wanted the results to be. Link to comment https://forums.phpfreaks.com/topic/237952-i-need-help-pulling-an-id-from-your-database/#findComment-1224492 Share on other sites More sharing options...
therocker Posted June 3, 2011 Author Share Posted June 3, 2011 This is the desired results, people have been telling me all these things, but they don't end up to be like this. This is how I want it to be, but no one seems to be giving me the right answer I need. This is just photoshopped so don't expect that I got it right. No one seems to understand what I'm trying to ask and so I just photoshopped this part so people can get a better understanding instead of giving me wrong results. Link to comment https://forums.phpfreaks.com/topic/237952-i-need-help-pulling-an-id-from-your-database/#findComment-1224498 Share on other sites More sharing options...
seanlim Posted June 3, 2011 Share Posted June 3, 2011 I, for one, have understood what you want from the start, and have nicely given you the solution: If you intend to print 1 ID for each post on the page, include the post-printing code within the while loop as well, to ensure that on each iteration of the loop, the ID is printed once together with the post. If you do not understand what I posted, simply ask nicely and I will elaborate. I don't know if you have asked for help outside of this thread, but it's no use venting your frustrations here. If you are not getting the help you want (for free) in this forums, suck it up. We don't owe you answers. Having cleared that up, I was suggesting placing the "post-printing" portion of your code in the while loop as such: while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { printf("<h2>#%s</h2>", $row["id"], $row["name"]); echo "<br /><div class=\"comment_gravatar left\"><img alt=\"\" src=\"http://photos-b.ak.fbcdn.net/hphotos-ak-snc6/229186_219276548101072_100000561868489_827154_8135363_n.jpg\" height=\"32\" width=\"32\" /></div><p>Posted By $name<br><br /><p>$message</p><br></div><div class=\"clr\"></div></div>"; } I don't know where/how your $name and $message fields are populated, but you will have to make sure that the appropriate values are assigned to them within the while loop for each post, so that the message is different below each ID. Link to comment https://forums.phpfreaks.com/topic/237952-i-need-help-pulling-an-id-from-your-database/#findComment-1224588 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.