Jump to content

Print List of Table with "SELECT DISTINCT"


equipment

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.