equipment Posted April 19, 2012 Share Posted April 19, 2012 I am trying to print the list of a table which I requested with "SELECT DISTINCT" as below $db_connect = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); $sql_get = "SELECT DISTINCT category FROM con"; $sql_run = mysqli_query($db_connect, $sql_get) or mysqli_error($db_connect); $sql_assoc = mysqli_fetch_assoc($sql_run); What is now needed to print the list of the table data by this conditions? I tried the while loop, but I seem to approach wrong, and get endless loops or errors. Quote Link to comment https://forums.phpfreaks.com/topic/261274-print-list-of-table-with-select-distinct/ Share on other sites More sharing options...
Maq Posted April 19, 2012 Share Posted April 19, 2012 Post the code you tried and the error messages you received. Quote Link to comment https://forums.phpfreaks.com/topic/261274-print-list-of-table-with-select-distinct/#findComment-1338886 Share on other sites More sharing options...
equipment Posted April 19, 2012 Author Share Posted April 19, 2012 I tried different approach, I just could not get it to work, though the SQL statement does work I've checked it with MySQL. For example: $sql_tags = $sql_assoc['category']; while($sql_tags) { echo $sql_tags; } Though it fails and it also causes an endless loop. Quote Link to comment https://forums.phpfreaks.com/topic/261274-print-list-of-table-with-select-distinct/#findComment-1338902 Share on other sites More sharing options...
Maq Posted April 19, 2012 Share Posted April 19, 2012 You need to put it in the while loop: while($sql_assoc = mysqli_fetch_assoc($sql_run)) { echo $sql_assoc['category']; } Quote Link to comment https://forums.phpfreaks.com/topic/261274-print-list-of-table-with-select-distinct/#findComment-1338918 Share on other sites More sharing options...
equipment Posted April 20, 2012 Author Share Posted April 20, 2012 Thanks a lot, it is a detail I missed, which does happen to me. I am wondering why does the while loop work when the assignment is put into the conditional checking? What happens if it is simply the variable? Quote Link to comment https://forums.phpfreaks.com/topic/261274-print-list-of-table-with-select-distinct/#findComment-1338950 Share on other sites More sharing options...
xyph Posted April 20, 2012 Share Posted April 20, 2012 If it is simply the variable, the conditional statement always evaluates to true. while( TRUE ){} will cause an infinite loop. Each time you call mysqli_fetch_assoc, it will return the NEXT row in the result set. When there are no rows left, it returns FALSE. So, each time the while loop checks it's conditionals, mysqli_fetch_assoc will either return the next row, or FALSE. When it returns FALSE, the conditional evaluates as FALSE, and the while stops looping. Quote Link to comment https://forums.phpfreaks.com/topic/261274-print-list-of-table-with-select-distinct/#findComment-1338954 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.