Jump to content

While Loop Leaves Out One Column Entry


equipment

Recommended Posts

$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?

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$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.

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.