Jump to content

Recommended Posts

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.

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.

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.

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.