开发者

Internal Server Error on a PHP app that is calling a function that calls MySQL database

开发者 https://www.devze.com 2023-03-17 14:33 出处:网络
Basically I am building a MVC app. In the Model, dbFunctions.php is this code: <?php require(\"config.php\");

Basically I am building a MVC app. In the Model, dbFunctions.php is this code:

<?php
require("config.php");
require("dbconnection.php");

class dbFunctions
{
    // setting up the object to connect to the Database
    function __construct() 
    {
        $dbConnect = new dbConnection(); 
    }

    public function insertPortfolioAdminData($value='')
    {
        # code...
    }

    public function login($value='')
    {
        # code...
    }

    public function logout($value='')
    {
        # code...
    }

    public function dbStoreContactForm($value='')
    {
        # code...
    }

    // returns a query with a collection of database objects for the portfolio
    public function fetchAllPortfolioItems()
    {
        $fetchAllPortfolioItemsReturnQry = mysql_query("SELECT description FROM PortfolioItems")  or die ('Error: '.mysql_error ()); 

        if($fetchAllPortfolioItemsReturnQry){
            return $fetchAllPortfolioItemsReturnQry; 
        }
    }

    public function fetchSinglePortfolioItems($primaryKey='')
    {
        # code...
    }

}
?> 

dbConnection.php

<?php 

require("config.php"); 

class dbConnection {

    private $databaseConnec开发者_如何学Pythontion;    

    public function dbConnection(){
        $databaseConnection = mysql_connect(dbHostName,dbUserName,dbPassword) or die(mysql_error());
        mysql_select_db(dbDatabaseName) or die(mysql_error());
    }

    public function closeConnection(){
        mysql_close($databaseConnection);
    }

}

?>

The controller:

<?php 
// Calling the class to do the work on database 
require("./model/dbfunctions.php"); 

$dbMethods = new dbFunctions(); 

while($row = mysql_fetch_array($dbMethods->fetchAllPortfolioItems()))
{
    $pageContent = $row["description"];
}

// calling the template
require("./views/page_12.php"); 

?> 

Here's the error:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@mvcportfolio.adambourg.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Basically I am trying to do all my DB work in the model then through the model pass it to the view to output it.


I see a few problems that may contribute to the error:

1 - You're using require for the "config.php" but you should really be using require_once. There's no need to load it more than once.

// Replace the require('config.php') with:
require_once('config.php');

2 - Are you defining constants in your "config.php"? The dbConnection::dbConnection() function is looking for constants named dbHostName, dbUserName, dbPassword, and dbDatabaseName. Make sure your "config.php" is defining constants.

3 - The while($row = mysql_fetch_array($dbMethods->fetchAllPortfolioItems())) in dbFunctions.php is wrong. The mysql_fetch_array portion of the while is "recalculated" on every iteration which means that it's executed over and over again. If you assign the value of $dbMethods->fetchAllPortfolioItems() to a variable first, the function is only executed once and the while will iterate through the results. Use this instead:

$result = $dbMethods->fetchAllPortfolioItems();

while($row = mysql_fetch_array($result))
{
    $pageContent = $row["description"];
}

As per the while documentation:

It tells PHP to execute the nested statement(s) repeatedly, as long as the while expression evaluates to TRUE.

The mysql_fetch_array($dbMethods->fetchAllPortfolioItems()) part of the while you're using will always evaluate to TRUE as long as the query return a row as it's getting called over and over (thus returning the same first row every single time).

The error that's causing the "Internal Server Error" is probably because your script is taking more than the max_execution_time allowed in php.ini.


It's maybe because you're never actually connecting to the database before you query it.

Essentially, you've made a function with the same name as the class for dbConnection instead of using the __construct method.


You will see a very clear error message in your server error log, in apache on linux: /var/log/apache/error.log
and I would also take a look (if it is not clear from the previews log) at the mysql error log, which is also somewhere under /var/log/...

0

精彩评论

暂无评论...
验证码 换一张
取 消