equipment Posted April 20, 2012 Share Posted April 20, 2012 $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); while($sql_assoc = mysqli_fetch_assoc($sql_run)){ echo $sql_assoc['category']; } From the column "category" I get printed 5 distinct entries, though the SQL statement itself does print out 6 distinct entries in MySQL, which is the right one. Just wondering why does the while loop leave out one entry? Also in the array is the "category" identifier necessary since it is identified in the SQL statement already? How would it look differently? Quote Link to comment https://forums.phpfreaks.com/topic/261302-while-loop-leaves-out-one-column-entry/ Share on other sites More sharing options...
MMDE Posted April 20, 2012 Share Posted April 20, 2012 Could it be the "DISTINCT" in the mysql query? To answer your last question, it results in an array, so yes, you must access it as an array. Quote Link to comment https://forums.phpfreaks.com/topic/261302-while-loop-leaves-out-one-column-entry/#findComment-1339027 Share on other sites More sharing options...
equipment Posted April 20, 2012 Author Share Posted April 20, 2012 As said the SQL statement works fine in MySQL. Regarding the array, is there are way to write out the array without the identifier, e.g. array[]. I am simply thinking to myself that the column is already identified in the SQL statement. Quote Link to comment https://forums.phpfreaks.com/topic/261302-while-loop-leaves-out-one-column-entry/#findComment-1339028 Share on other sites More sharing options...
MMDE Posted April 20, 2012 Share Posted April 20, 2012 As said the SQL statement works fine in MySQL. Regarding the array, is there are way to write out the array without the identifier, e.g. array[]. I am simply thinking to myself that the column is already identified in the SQL statement. If you only want to get one specified result: mysql_result Quote Link to comment https://forums.phpfreaks.com/topic/261302-while-loop-leaves-out-one-column-entry/#findComment-1339029 Share on other sites More sharing options...
kicken Posted April 20, 2012 Share Posted April 20, 2012 $sql_assoc = mysqli_fetch_assoc($sql_run); while($sql_assoc = mysqli_fetch_assoc($sql_run)){ Your first call to mysqli_fetch_assoc there is returning the first row, but you just drop it and never echo that row out. So if your seeing 5 results echoed, that means your getting 6 returned, but your ignoring the first one. Quote Link to comment https://forums.phpfreaks.com/topic/261302-while-loop-leaves-out-one-column-entry/#findComment-1339031 Share on other sites More sharing options...
equipment Posted April 20, 2012 Author Share Posted April 20, 2012 kicken, are you addressing the conditional check, I am not clearly seeing where the "drop" is happening as you call it, though I do understand the logic of your writing. Quote Link to comment https://forums.phpfreaks.com/topic/261302-while-loop-leaves-out-one-column-entry/#findComment-1339033 Share on other sites More sharing options...
trq Posted April 20, 2012 Share Posted April 20, 2012 Remove this line: $sql_assoc = mysqli_fetch_assoc($sql_run); Quote Link to comment https://forums.phpfreaks.com/topic/261302-while-loop-leaves-out-one-column-entry/#findComment-1339034 Share on other sites More sharing options...
equipment Posted April 20, 2012 Author Share Posted April 20, 2012 Thanks a lot, it is working now, I did miss that the function is fetching it and then assigning it, though it does get regarded when executing the while loop. What about the printing of the array, is the identifier of the column name really necessary when it is already identified in the SQL query? Quote Link to comment https://forums.phpfreaks.com/topic/261302-while-loop-leaves-out-one-column-entry/#findComment-1339052 Share on other sites More sharing options...
kicken Posted April 20, 2012 Share Posted April 20, 2012 What about the printing of the array, is the identifier of the column name really necessary when it is already identified in the SQL query? Yes, because the array could contain multiple columns if you were to select multiple columns. As such you have to specify which one you want to access within the array by using the key name. Quote Link to comment https://forums.phpfreaks.com/topic/261302-while-loop-leaves-out-one-column-entry/#findComment-1339059 Share on other sites More sharing options...
equipment Posted April 20, 2012 Author Share Posted April 20, 2012 Why would the array contain multiple columns, when only one has been selected in the SQL query? Quote Link to comment https://forums.phpfreaks.com/topic/261302-while-loop-leaves-out-one-column-entry/#findComment-1339085 Share on other sites More sharing options...
kicken Posted April 20, 2012 Share Posted April 20, 2012 Why would the array contain multiple columns, when only one has been selected in the SQL query? In this specific case, it won't contain multiple columns. The API is a generic solution though which is designed to handle the case of multiple columns. Even if the query only returns one column the code is still going to generate an array with a key for the column, and when you access it you will have to specify the key. Quote Link to comment https://forums.phpfreaks.com/topic/261302-while-loop-leaves-out-one-column-entry/#findComment-1339127 Share on other sites More sharing options...
equipment Posted April 20, 2012 Author Share Posted April 20, 2012 Why would the array contain multiple columns, when only one has been selected in the SQL query? [...] Even if the query only returns one column the code is still going to generate an array with a key for the column, and when you access it you will have to specify the key. Should you not be specifying the column entries rather than the column name itself? Because after all the column entries are at quantity and would have to be selected if necessary, whereas the column is only one with distinctively selected entries all assigned to the array. Would that not make more sense? Since PHP the language is trying to be as efficient as possible. Quote Link to comment https://forums.phpfreaks.com/topic/261302-while-loop-leaves-out-one-column-entry/#findComment-1339148 Share on other sites More sharing options...
equipment Posted April 21, 2012 Author Share Posted April 21, 2012 Addendum: I misunderstood your posts, I do understand now, after thinking it through, by what you meant with "multiple columns", it can represent a series in an array, as in an array in an array, and this that one somehow needs to be defined as well. Quote Link to comment https://forums.phpfreaks.com/topic/261302-while-loop-leaves-out-one-column-entry/#findComment-1339238 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.