With WordPress to make a custom SQL query, you should always use the dedicated WPDB
Class and related methods as get_results()
, this way:
global $wpdb;
$results = $wpdb->get_results( "
SELECT attribute_name
FROM {$wpdb->prefix}woocommerce_attribute_taxonomies
" );
if ( count($results) > 0) {
$data = []; // Initializing
// Loop through results objects
foreach( $results as $result ) {
// add each value to an array
$data[] = $result->attribute_name . "<br>";
}
// output data of all rows
echo implode( "<br>", $data );
} else {
echo "0 results";
}
Better, when you are querying one column of a table (as you are doing), you can use get_col()
method this way:
global $wpdb;
$results = $wpdb->get_col( "
SELECT attribute_name
FROM {$wpdb->prefix}woocommerce_attribute_taxonomies
" );
if ( count($results) > 0) {
// output data of all rows
echo implode( "<br>", $results );
} else {
echo "0 results";
}
The best easy way is to use WooCommerce wc_get_attribute_taxonomies()
dedicated function:
$output="<ul style="list-style:none;">";
// Loop through all Woocommerce product attributes
foreach( wc_get_attribute_taxonomies() as $attribute ) {
$attribute_label = $attribute->attribute_label; // Product attribute name
$attribute_slug = $attribute->attribute_name; // Product attribute slug
$output .= '<li class="'.$attribute_slug.'">' . $attribute_label . '</li>';
}
// Output
echo $output . '</ul>';
Related thread: Get woocommerce product attributes to sidebar