surreal5335 Posted January 5, 2010 Share Posted January 5, 2010 I am trying to loop through a series of database fields and have print out on the web page. My one function that prints out just a specific group of fields works fine: /** * returns array of a post from database * @returns array */ function find_post($id) { $connection = db_connect(); $query = sprintf("select posts.title, posts.body, posts.user_id, users.name from posts, users where posts.user_id = user_id and posts.id = %s", mysql_real_escape_string($id) ); $result = mysql_query($query); $number_of_posts = @mysql_num_rows($result); if ($number_of_posts == 0) { return false; } $row = mysql_fetch_array($result); return $row; } But my function that has problems getting the data from the database is: /** * returns array of posts from database * @returns array */ function find_posts() { $connection = db_connect(); $query = 'select posts.title, posts.body, posts.user_id, users.name from posts, users where posts.user_id = user_id'; $result = mysql_query($query); $number_of_posts = mysql_num_rows($result); if ($number_of_posts == 0) { return false; } $result = result_to_array($result); return $result; } While I am not getting any errors, I am also not getting any data from my database either I have already had a problem with this function about the function it calls within it: function result_to_array($result) { $result_array = array(); for ($i=0; $row = @mysql_fetch_array($result); $i++) { $result_array[$i] = $row; } return $result_array; } While I am feeling good about being able to fix a majority of my errors, this one I am not sure about. I appreciate any help in the matter Quote Link to comment https://forums.phpfreaks.com/topic/187312-problem-looping-through-database-fields/ Share on other sites More sharing options...
kickstart Posted January 5, 2010 Share Posted January 5, 2010 Hi Can't see anything obvious. I would suggest putting an "or die(mysql_error)" on the end of the mysql_query. One possible issue is that posts.user_id = user_id doesn't specify the table for the 2nd column (which isn't unique). All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/187312-problem-looping-through-database-fields/#findComment-989181 Share on other sites More sharing options...
surreal5335 Posted January 6, 2010 Author Share Posted January 6, 2010 Thanks for the help, I can now come to safe conclusion that find_posts() is not the problem. well I spilled over my code for a couple hours and turned on php error reporting. These are the set of notices I am getting: Notice: Undefined variable: post in /home1/royalvil/public_html/post_ads/_post.php on line 7 Notice: Undefined variable: post in /home1/royalvil/public_html/post_ads/_post.php on line 11 Notice: Undefined variable: post in /home1/royalvil/public_html/post_ads/_post.php on line 15 currently the code for that page is as follows: <div class="post"> <h2><?php echo $post['title']; ?></h2> <span class="body-text"> <?php echo $post['body']; ?> </span> <span class="username"> <?php echo $post['username']; ?> </span> <span class="email"> <?php echo $post['email']; ?> </span> </div> right now my code for calling the looping function is being assigned to $posts. I have tried many ways to make it work as $post so the code above will get the info from find_posts() properly. When ever I try to change it over I get this notice: Notice: Undefined index: body in /home1/royalvil/public_html/post_ads/_post.php on line 6 Notice: Undefined index: username in /home1/royalvil/public_html/post_ads/_post.php on line 10 Notice: Undefined index: email in /home1/royalvil/public_html/post_ads/_post.php on line 14 the only place I can find reference to $posts in my code is here: //$posts = find_posts(); foreach($posts as $post) { echo '<h2>'.$post['title'].'</h2><br/>'; echo $post['body'].'<br/>'; echo $post['email'].'<br/>'; echo $post['name'].'<br/>'; } keep in mind these code snipets are spread out over several pages. Now this may seem like the answer, but I have tried leaving them uncommented so they would active, but that made no difference. Does anyone have any suggestins as to what I can do? Thanks a lot for the help Quote Link to comment https://forums.phpfreaks.com/topic/187312-problem-looping-through-database-fields/#findComment-989453 Share on other sites More sharing options...
kickstart Posted January 6, 2010 Share Posted January 6, 2010 Hi Still can't see anything obvious, but it does appear your $post array isn't being fully populated as you expect it to be. I would spread a few echo statements around the functions collecting the data to check exactly what is being brought back. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/187312-problem-looping-through-database-fields/#findComment-989499 Share on other sites More sharing options...
surreal5335 Posted January 6, 2010 Author Share Posted January 6, 2010 well I tried the suggestion you made. When I did that I got this output: Array I also changed a bit of code in find_posts() as someones suggestion changing: $result = result_to_array($result); to this: $row = mysql_fetch_array($result); which gave me an output of: Resource id #7 not really sure what to make of this any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/187312-problem-looping-through-database-fields/#findComment-989817 Share on other sites More sharing options...
kickstart Posted January 6, 2010 Share Posted January 6, 2010 Hi Think I would need to see a lot more of the code. The message you are getting suggests to me that you are trying to echo out a variable that contains the returned recordset, rather than an array of fields. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/187312-problem-looping-through-database-fields/#findComment-989831 Share on other sites More sharing options...
tomdchi Posted January 7, 2010 Share Posted January 7, 2010 try print_r($arrayname); at various points in your code to see if the arrays are getting populated as expected. Quote Link to comment https://forums.phpfreaks.com/topic/187312-problem-looping-through-database-fields/#findComment-990162 Share on other sites More sharing options...
surreal5335 Posted January 7, 2010 Author Share Posted January 7, 2010 Thanks a lot for the suggestion, I dont know why I didnt think of it before. I used this code: $posts = find_posts(); print_r($posts); which gave me this output: Array ( [0] => Array ( [0] => painter needed for siding [title] => painter needed for siding [1] => I need a painter to paint the side of my house. pay is negotiable. paid lunches. [body] => I need a painter to paint the side of my house. pay is negotiable. paid lunches. [2] => 1 [user_id] => 1 [3] => super_dude [name] => super_dude ) [1] => Array ( [0] => carpet layer needed [title] => carpet layer needed [1] => I need a carpet layer that has good skills with stairs [body] => I need a carpet layer that has good skills with stairs [2] => 1 [user_id] => 1 [3] => super_dude [name] => super_dude ) ) it seems obvious that the data is beng gathered into the array from the database, but is failing to get echoed in the html layout I created. I believe this problem to be because find_posts() is assigned to $posts. While the data echoed out in the html layout is looking for data in $post not $posts. Which also explains why I am getting this notice from php error reporting: Notice: Undefined variable: post In this url route all the database data is assigned to $posts. My other url route that serves a different function uses $post which works fine with the html layout code. My hope is to be able to assign find_posts() to $post without getting this message from php: Notice: Undefined index: body body is a field name in my database if there was any confusion. Does any one have any suggestions on how to accomplish this? Quote Link to comment https://forums.phpfreaks.com/topic/187312-problem-looping-through-database-fields/#findComment-990171 Share on other sites More sharing options...
kickstart Posted January 7, 2010 Share Posted January 7, 2010 Hi This is the problem with dealing with small fragments of code. foreach($posts as $post) { echo '<h2>'.$post['title'].'</h2><br/>'; echo $post['body'].'<br/>'; echo $post['email'].'<br/>'; echo $post['name'].'<br/>'; } will work fine. It is looping round each occurance of $posts and assigning each in turn to $post. As such that will be fine. However I cannot tell if the other fragment you posted earlier which refers to $post is within a similar loop. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/187312-problem-looping-through-database-fields/#findComment-990185 Share on other sites More sharing options...
surreal5335 Posted January 8, 2010 Author Share Posted January 8, 2010 Thanks for the great suggestion, got me out of the woods and into more comfortable territory. the suggestion yu made corrected my current problem but messed up the other single post that didnt loop using $post as a variable. So I made an if test to check the url: if ($route['view'] == 'index') { foreach($posts as $post) { include('_post.php'); } } else { include('_post.php'); } _post.php has the html layout in it. Thanks a lot for pointing me in the right direction! Quote Link to comment https://forums.phpfreaks.com/topic/187312-problem-looping-through-database-fields/#findComment-990782 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.