Home > database >  Constant DB already define
Constant DB already define

Time:09-29

Hai i want to ask about my problem. So i have a several function to get data from database. And for that i have a connection database. My several function located in one file function. And when i run the code i have a message notification like this. enter image description here

And this is for my function code

function getDataToko()
{
    $db = getDBConnection();

    $query = "SELECT * FROM master_toko WHERE status_ot='1' ORDER BY nama_ot DESC";
    $result = mysqli_query($db, $query);
    mysqli_close($db);
    return $result;
}

function getDataAkses()
{
    $db = getDBConnection();

    $query = "SELECT tk.nama_ot, ha. * FROM master_toko AS tk, master_hak_akses AS ha WHERE tk.id_ot = ha.id_ot AND ha.status_ha = '1'";
    $result = mysqli_query($db, $query);
    mysqli_close($db);
    return $result;
}

And this for the page html i called the function

<select class="form-control select2bs4" name="namaToko" required>
                            <option disabled="disabled" selected="selected">Pilih Toko</option>
                            <?php $toko = getDataToko();  ?>
                            <?php
                            while ($dataToko = mysqli_fetch_array($toko)) {
                            ?>
                              <option value="<?= $dataToko['id_ot'] ?>">
                                <?= $dataToko['nama_ot'] ?>
                              </option>
                            <?php
                            }
                            ?>
                          </select>
<tbody>
                    <?php
                    $n = 1;
                    $akses = getDataAkses();
                    while ($dataAkses = mysqli_fetch_array($akses)) {
                      if ($dataAkses['status_ha'] == 1) {
                        $status = "Aktif";
                        $idStatus = "1";
                      }
                      if ($dataAkses['status_ha'] == 2) {
                        $status = "Tidak Aktif";
                        $idStatus = "2";
                      }

                    ?>
                      <tr data-row-id="<?= $dataAkses['id_ha'] ?>">
                        <td>
                          <?php echo $n  ; ?>
                        </td>
                        <td>
                          <?php echo $dataAkses['id_ha'] ?>
                        </td>
                        <td>
                          <?php echo $dataAkses['nama_ot'] ?>
                        </td>
                        <td>
                          <?php echo $dataAkses['nama_ha'] ?>
                        </td>
                        <td>
                          <?php echo $status ?>
                        </td>
                        <td>
                          <button type="button" class="view-detail btn btn-success" data-id="<?= $dataToko['id_ot'] ?>"> Edit </button>
                          <!-- <button onclick="document.getElementById('delete').style.display='block'" class="view-delete btn btn-danger">Delete</button> -->
                          <button type="button" class="view-delete btn btn-danger" data-id="<?= $dataToko['id_ot'] ?>" id="deleteButton"> Delete </button>
                          <?php
                          include 'hapus_toko.php';
                          ?>
                        </td>
                      </tr>
                    <?php
                      include 'edit_toko.php';
                    }
                    ?>

                  </tbody>

This my config.php

<?php
    function getDBConnection() {
    define('DB_SERVER', 'localhost');
    define('DB_USERNAME', 'root');
    define('DB_PASSWORD', '');

    define('DB_DATABASE', 'dbcity');
    $db=mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

    if(!$db) {
        die("<script>alert('Gagal tersambung dengan database.')</script>");
    }
    return $db;
}

?>

Can anyone help me for this bug?

CodePudding user response:

This is happening because your define is inside getDBConnection, which you call more than once. You need to move the definitions outside the function at the top of config.php

  • Related