【问题标题】:Connecting database locally using MAMP使用 MAMP 在本地连接数据库
【发布时间】:2017-09-11 10:39:48
【问题描述】:

我正在使用此代码进行连接:

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

 ?>

这是我的 db_credentials.php 文件:

  <?php

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

我使用一个简单的 assoc 语句来显示我的名为 subject 的表,但我在网站上根本没有显示。

我想我可能在我的 db_credentials 中写错了信息

我有我的数据库信息的屏幕截图: ?>

我创建了一个名为 hej 的用户,密码为 123,并且该用户已被授予所有访问权限。

这是我的索引文件,我试图在其中显示名为主题的表:

 <?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'); ?>

【问题讨论】:

  • 我想我可能在我的 db_credentials 中写了错误的信息 - 考虑输入正确的凭据或至少在您发布问题之前确认您的怀疑,以便我们更轻松地发布问题?
  • 嗯,我使用的凭据是我认为正确的凭据,但由于我没有收到任何输入,我怀疑我可能误解了要使用的正确凭据是什么。 (这就是为什么我包括了截图和用户信息,以防我误解了)

标签: php mysqli mamp


【解决方案1】:

连接到localhost 时,默认情况下(除非更改)用户凭据是以下组合:

root/root
root/

第一个值是用户名,第二个值是密码。

注意:第二个密码值有意留空以表示空字符串。

【讨论】:

  • 我包含了我的索引文件,但是我仍然怀疑我误解了正确使用 mysql,因为我今天才开始了解它..
  • 我在编写 root/root 后错过了一个语法。感谢您的帮助!
猜你喜欢
  • 2013-09-20
  • 2019-03-03
  • 1970-01-01
  • 2013-02-22
  • 2017-09-20
  • 2020-04-17
  • 1970-01-01
  • 2016-09-10
  • 2016-12-20
相关资源
最近更新 更多