programguru Posted February 20, 2009 Share Posted February 20, 2009 i cannot get my results to be 4 or less, and i don't know why my script will not do this! all i can get it to do is print one record when it should be printing up to 4 that meet my criteria as you can see in my mysql query. // test featured only list $table_name = "special"; $bd_string_featured = "<table> <tr> <th>Date Added</th> <th>Date Start</th> <th>Date End</th> <th>Title</th> <th>View</th> <th>Edit</th> <th>Delete</th> <th>Featured</th> </tr> "; $sql = "SELECT id, date_added, date_start, date_end, title, spaw1, featured FROM $table_name WHERE featured=1 ORDER by date_added"; $result = mysql_query($sql) or die(mysql_error()); $num = mysql_num_rows($result); echo $num; while ($num <= 4) { $row = mysql_fetch_array($result); $id = $row['id']; $date_added = $row['date_added']; $date_start = $row['date_start']; $date_end = $row['date_end']; $title = $row['title']; $featured = $row['featured']; $bd_string_featured .= '<tr> <td> ' . $date_added .' </td> <td> ' . $date_start .' </td> <td> ' . $date_end .' </td> <td><a href="app_view_special.php?id=' . $id . '">' . $title . '</a></td> <td><a href="app_view_special.php?id=' . $id . '">View</a></td> <td><a href="review_edit_special.php?id=' . $id . '">Edit</a></td> <td><a href="javascript:deleteConfirmation(\'app_delete_special.php?id=' . $id . '\')">Delete</a></td> <td>' . $featured . '</td></tr>'; $bd_string_featured .= "</table>"; $num++; } ?> Link to comment https://forums.phpfreaks.com/topic/146083-solved-records/ Share on other sites More sharing options...
genericnumber1 Posted February 20, 2009 Share Posted February 20, 2009 You're setting $num to being mysql_num_rows(), so 4. Then you're doing while(4 <= 4) do something then increase $num. This results in 5 <= 4 so it breaks. the easiest method is instead of using mysql_num_rows() just do.. $result = mysql_query($sql) or die(mysql_error()); // Code removed here while ($row = mysql_fetch_array($result)) { // And here (moved up to the where clause) $id = $row['id']; Link to comment https://forums.phpfreaks.com/topic/146083-solved-records/#findComment-766885 Share on other sites More sharing options...
programguru Posted February 20, 2009 Author Share Posted February 20, 2009 You're setting $num to being mysql_num_rows(), so 4. Then you're doing while(4 <= 4) do something then increase $num. This results in 5 <= 4 so it breaks. the easiest method is instead of using mysql_num_rows() just do.. $result = mysql_query($sql) or die(mysql_error()); // Code removed here while ($row = mysql_fetch_array($result)) { // And here (moved up to the where clause) $id = $row['id']; genericnumber1, thanks for the insight.. i feel like a moron after 3 hours or trying to figure this out.. 4 <= 4 man what was I thinking! still learning... im going to test it in the am, but was this is what you mean? ... $result = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_array($result); while ($row = mysql_fetch_array($result)) { $id = $row['id']; ... Link to comment https://forums.phpfreaks.com/topic/146083-solved-records/#findComment-766888 Share on other sites More sharing options...
genericnumber1 Posted February 20, 2009 Share Posted February 20, 2009 You don't even need that first mysql_fetch_array(). The while loop conditional will retrieve the rows until it has no more to retrieve, then mysql_fetch_array() will return false and terminate the loop. Link to comment https://forums.phpfreaks.com/topic/146083-solved-records/#findComment-766894 Share on other sites More sharing options...
programguru Posted February 20, 2009 Author Share Posted February 20, 2009 You don't even need that first mysql_fetch_array(). The while loop conditional will retrieve the rows until it has no more to retrieve, then mysql_fetch_array() will return false and terminate the loop. i see what you mean.. the results are perfect now, but i just need to limit the results to a max of 3 less than or equal to 3... messing around with that now, and more errors if you have any idea on this, please feel free to throw your advice Link to comment https://forums.phpfreaks.com/topic/146083-solved-records/#findComment-766897 Share on other sites More sharing options...
genericnumber1 Posted February 20, 2009 Share Posted February 20, 2009 It's easier to add it to your query.. SELECT id, date_added, date_start, date_end, title, spaw1, featured FROM $table_name WHERE featured=1 ORDER by date_added LIMIT 3 Link to comment https://forums.phpfreaks.com/topic/146083-solved-records/#findComment-766904 Share on other sites More sharing options...
programguru Posted February 20, 2009 Author Share Posted February 20, 2009 i was thinking there might be a query that would do that.. but again you beat me to it! thanks for the advice. Link to comment https://forums.phpfreaks.com/topic/146083-solved-records/#findComment-766907 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.