【问题标题】:Linking sql database table using Mamp with php使用 Mamp 与 php 链接 sql 数据库表
【发布时间】:2018-10-20 14:45:53
【问题描述】:

我正在使用 Mamp 中的 php 和数据库制作基于文本的冒险游戏。

我已将房间描述放入一个表格中,每个房间都有一个 ID 号。但是,我无法从 sql 中获取表,我总是收到 HTTP ERROR 500。

如果我取出 $sql - ?>,我知道数据库会在页面加载时连接。

php 代码如下:

<?php

// Extract the page data from the d/b
// define the constants
// note best practice is to keep these separate so values can be 
changed easily
$servername = "localhost";
$username = "root";
$password = "1234";
$dbname = "Room";

// Create connection using the above values
$conn = mysqli_connect($servername, $username, $password, $dbname);
// extract values from d/b
$sql = 'SELECT * FROM Room, Rooms'; // build the query string
$result = $conn->query($sql);   // execute the query - sends back a 
list of records
$row = $result->fetch_assoc();      // select the first row/record
$description = $row['RoomDesc'];    // select the description of the 
room
?>

【问题讨论】:

  • 'changed easy' 是上一个注释的一部分
  • 有几个 cmets 已经断线,也许编辑问题以将它们放在一条线上?另外 - 我已经添加了一个答案,如果这有帮助,请告诉我:)
  • 如果您找到了问题的答案,请将其标记为“已接受”
  • 你有一个错字。如果您在 db Room 内有一个表 Rooms,则它是 Room.Rooms。无论如何房间,房间是无效的mysql

标签: php sql database mamp


【解决方案1】:

$conn 是指向数据库的链接,而不是 MySQLi 类型的对象,您 500 因为您试图在非对象上调用函数,相反,您需要执行以下操作;

$result = mysqli_query($conn, $sql);   // execute the query - sends back a list of records

除此之外,获取记录的功能是;

$row = $result->fetchAssoc();

这是因为您使用的是过程而不是 OOP 方法

您还可以使用以下内容查看您遇到的错误;

ini_set("display_errors", "on");
error_reporting(E_ALL);

根据 Q 上的 cmets,您在 SQL 中有错误,您执行SELECT * FROM Room, Rooms 这是无效的,您只能使用一个 FROM,但是您可以连接表,修复您的 SQL,您将修复你的问题

没有看到数据库 - 我们无法帮助修复它

【讨论】:

  • 还是报同样的错误,不知道怎么实现底部代码看具体的错误。
猜你喜欢
  • 2016-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-10
  • 1970-01-01
  • 2019-03-29
相关资源
最近更新 更多