OldWest Posted November 2, 2010 Share Posted November 2, 2010 I am working to echo the results in a while or for loop... Both of my sample codes work, but the results are wrong! The while loop ONLY echos a result IF the first record in the postings table matches the id passed (does not display a result unless the first record has a match) The if loop displays ALL listings with the same name (counts them all) so there are no unique listings! <?php $posts_by_city_sql = "SELECT * FROM postings WHERE id='$_GET[id]'"; $posts_by_city_results = (mysqli_query($cxn, $posts_by_city_sql)) or die("Was not able to grab the Postings!"); /* While Loop */ while($posts_by_city_row = mysqli_fetch_array($posts_by_city_results)) { echo "<li><a href='posting_details.php?id=$posts_by_city_row[id]'>$posts_by_city_row[title]</a></li>"; } /* For Loop */ $posts_by_city_row = mysqli_fetch_array($posts_by_city_results); for ($i=0; $i<sizeof($posts_by_city_row); $i++) { echo "<li><a href='posting_details.php?id=$posts_by_city_row[id]'>$posts_by_city_row[title]</a></li>"; } ?> Results with for loop (there are 7 total unique book names, but it's just counting the first match on id 7 times like below): AJAX for Beginners AJAX for Beginners AJAX for Beginners AJAX for Beginners AJAX for Beginners AJAX for Beginners AJAX for Beginners AJAX for Beginners Quote Link to comment https://forums.phpfreaks.com/topic/217526-for-loop-while-loop-working-but-results-are-wrong-in-both/ Share on other sites More sharing options...
trq Posted November 2, 2010 Share Posted November 2, 2010 You are misunderstanding what mysqli_fetch_array returns. Each time it is called it returns an array containing each field from 1 row of your database and moves the pointer to the next row where it waits for the next call. Your 'while' loop is likely only returning 1 row because that is all that matched your query. Your 'for' loop doesn't make much sense because it loops through each column within a single row and simply displays the same data each time through. Quote Link to comment https://forums.phpfreaks.com/topic/217526-for-loop-while-loop-working-but-results-are-wrong-in-both/#findComment-1129324 Share on other sites More sharing options...
OldWest Posted November 2, 2010 Author Share Posted November 2, 2010 You are misunderstanding what mysqli_fetch_array returns. Each time it is called it returns an array containing each field from 1 row of your database and moves the pointer to the next row where it waits for the next call. Your 'while' loop is likely only returning 1 row because that is all that matched your query. Your 'for' loop doesn't make much sense because it loops through each column within a single row and simply displays the same data each time through. I very much appreciate your forwardness and accuracy. It was of course my error. See I have two tables I am testing with: Cities and Postings Postings belong to cities. And cities have postings of course. In postings, I have field city_id (obviously to be a key for the city id field) When I was running my query like so: $posts_by_city_sql = "SELECT * FROM postings WHERE id='$_GET[id]'"; . I was supposed to be SQLing ... WHERE city_id='$_GET[id] ... (note city_id in place of id) .. My script works as expected now. Quote Link to comment https://forums.phpfreaks.com/topic/217526-for-loop-while-loop-working-but-results-are-wrong-in-both/#findComment-1129555 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.