简体   繁体   中英

Constant DB already define

I have several functions to get data from the database. And for that, I have a connection database. My several functions are located in one file function. And when I run the code I have a message notification like this. 在此处输入图像描述

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;
}

?>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM