0

I'm trying to pull information from my database based on the values of a specific column. They're being pulled into different categories based on this column, but some of the rows have to be pulled several times as they are part of several categories or "sections". I'm using the datatype SET for the category column so I can set multiple sections easily.

    $code_sections = array (
    'Section 1' => 'section_1',
    'Section 2' => 'section_2',
    'Section 3' => 'section_3',
    'Section 4' => 'section_4',
    'Section 5' => 'section_5',
    'Section 6' => 'section_6',
    'Section 7' => 'section_7',
    );

    foreach($code_sections as $name => $section) {
        $query="SELECT * FROM `attributes` WHERE `section`= '$section' ";
        $results = mysqli_query($conn, $query) or exit(mysqli_error());
        
            echo '<h3>' . $name . '</h3>';
            echo '<div class="auto-grid">';

            while ($row = mysqli_fetch_array($results)) {
                echo    '$row["name"]';
                }

            echo '</div>';
    }

This works if I have data in the column that matches the array exactly, which is basically when the row is only part of ONE section. If it's part of two sections it will not be pulled at all.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Chrazini
  • 25
  • 1
  • 6
  • _If it's part of two sections_ in your mysql table how you store that row is in two section? – Simone Rossaini Jul 02 '20 at 09:47
  • could you not try a single query, such as `select * from attributes where section in ( "section_1","section_2","section_3","section_4","section_5","section_6","section_7" ) order by section` ? – Professor Abronsius Jul 02 '20 at 09:59
  • You have an error. [`mysqli_error()`](https://www.php.net/manual/en/mysqli.error.php) needs one argument. Please consider switching error mode on instead. [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Jul 02 '20 at 16:51

1 Answers1

0

The mysql Documentation may help you https://dev.mysql.com/doc/refman/8.0/en/set.html

Normally, you search for SET values using the FIND_IN_SET() function or the LIKE operator:

mysql> SELECT * FROM tbl_name WHERE FIND_IN_SET('value',set_col)>0;

mysql> SELECT * FROM tbl_name WHERE set_col LIKE '%value%';
Squall
  • 118
  • 1
  • 9