简体   繁体   中英

Linking sql database table using Mamp with php

I am making a text-based adventure game with php and databases in Mamp.

I have put the room descriptions into a table with an id number for each room. However, I cant get the table from sql, I always get a HTTP ERROR 500.

I know the database connects as the page loads if I take out $sql - ?>.

The php code looks like:

<?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
?>

$conn is a link to the database, not an object of type MySQLi, you 500 because you are trying to call a function on a non-object, instead, you need to do something such as;

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

As well as this, the function to get records is;

$row = $result->fetchAssoc();

This is because you are using procedural and not the OOP methodology

You can also use the following to see what errors you are getting;

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

As per comments on Q, you have an error in the SQL, you do SELECT * FROM Room, Rooms which is not valid, you can only use one FROM , you can join tables however, fix your SQL and you'll fix your issues

Without seeing the database - we can't help with fixing that

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