Search code examples
phpmysqlimamp

Connecting database locally using MAMP


I am using this code to connect:

     require_once('db_credentials.php');

     function db_connect() {
         $connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
             return $connection;
            }

  function db_disconnect($connection) {
      if(isset($connection)) {
      mysqli_close($connection);
    }
  }

 ?>

here is my db_credentials.php file:

  <?php

  define("DB_SERVER", "localhost");
  define("DB_USER", "");
   define("DB_PASS", "");
  define("DB_NAME", "globe_bank");

I use a simple assoc statement to show my table called subjects, but I get no display at all on the website.

I think I might have written wrong infomation in my db_credentials

I have a screen shot of the information of my database: enter image description here ?>

I created an user called hej with the password 123, and the user has been granted all access.

This is my index file where I'm trying to show my table called subjects:

 <?php require_once('../../../private/initialize.php'); ?>

       <?php

     $subject_set = find_all_subjects();

     ?>

     <?php $page_title = 'Subjects'; ?>
      <?php include(SHARED_PATH . '/staff_header.php'); ?>

      <div id="content">
        <div class="subjects listing">
          <h1>Subjects</h1>

          <div class="actions">
            <a class="action" href="<?php echo url_for('/staff/subjects/new.php'); ?>">Create New Subject</a>
</div>

<table class="list">
  <tr>
    <th>ID</th>
    <th>Position</th>
    <th>Visible</th>
    <th>Name</th>
    <th>&nbsp;</th>
    <th>&nbsp;</th>
    <th>&nbsp;</th>
  </tr>

  <?php while($subject = mysqli_fetch_assoc($subject_set)) { ?>
    <tr>
      <td><?php echo h($subject['id']); ?></td>
      <td><?php echo h($subject['position']); ?></td>
      <td><?php echo $subject['visible'] == 1 ? 'true' : 'false'; ?></td>
        <td><?php echo h($subject['menu_name']); ?></td>
      <td><a class="action" href="<?php echo url_for('/staff/subjects/show.php?id=' . h(u($subject['id']))); ?>">View</a></td>
      <td><a class="action" href="<?php echo url_for('/staff/subjects/edit.php?id=' . h(u($subject['id']))); ?>">Edit</a></td>
      <td><a class="action" href="">Delete</a></td>
      </tr>
  <?php } ?>
</table>

<?php
  mysqli_free_result($subject_set);
          ?>
        </div>

      </div>

      <?php include(SHARED_PATH . '/staff_footer.php'); ?>

Solution

  • When connecting to localhost the user credentials by default (unless changed) are either a combination of the following:

    root/root
    root/
    

    Where the first value is the username and second value is the password.

    Note: The second password value is intentionally left blank to signify an empty string.