简体   繁体   中英

Internal Server Error with linking database in PHP

I have a database that I want to use to make an array for an app I'm working on in Xcode. I'm following a tutorial I found on the internet here , and I followed the instructions, but my page is displaying an Internal Server Error. I am assuming that I didn't correctly change the info in the following code. Also, my web host uses MYSQL not MYSQLite, so I'm not sure what to change the "new mysqli" to in the code. For obvious reasons I omitted the database info. The database has one table named "descriptions" and the columns are labeled 1-50 with different values assigned.

<?php

class RedeemAPI {
    private $db;

    // Constructor - open DB connection
    function __construct() {
        $this->db = new mysqli('hostname', 'Username', 'Pass', 'Database Name');
        $this->db->autocommit(FALSE);
    }

    // Destructor - close DB connection
    function __destruct() {
        $this->db->close();
    }

    // Main method to redeem a code
    function redeem() {
        // Print all codes in database
        $stmt = $this->db->prepare('SELECT 1 FROM descriptions');
        $stmt->execute();
        $stmt->bind_result($1);
        while ($stmt->fetch()) {
            echo "$1";
        }
        $stmt->close();
    }
}

// This is the first thing that gets called when this page is loaded
// Creates a new instance of the RedeemAPI class and calls the redeem method
$api = new RedeemAPI;
$api->redeem();

?>

PHP variables must start with a letter or _ , so your error stems from this statement:

$stmt->bind_result($1);

Change the variable name to something like $col1, or whatever is meaningful.

Also, your select statement will return a single value: 1. You probably mean to do this:

SELECT * FROM descriptions

Two other points:

  • You shouldn't disable autocommit unless you intend to use transactions.
  • There's no need to prepare statements with simple queries that don't include untrusted input. You can just use $this->db->query("...")'. You'd need to tweak the other statements to match if you change this.

That internal server error 500 is probably masking some lovely error messages that may help. Switch your debugging on in your php.ini / php5.ini

Add this to your php.ini / php5.ini or just add one of these files to the root of your website should do the trick

display_errors = On
display_startup_errors = On
error_reporting = -1

Second, if your host is not running MYSQLi, then they are running REALLY old php as MYSQLi is standard in php4.1.3 and above

Third, that SQL statement.. its it just some kind of rough example or is that your actual statement? If you are trying to pull off one record it should be

SELECT * FROM tablename.description LIMIT 1

Always best to make your statements very thorough

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