[英]Internal Server Error on a PHP app that is calling a function that calls MySQL database
Basically I am building a MVC app.基本上我正在构建一个 MVC 应用程序。 In the Model, dbFunctions.php is this code:在 Model 中,dbFunctions.php 是这个代码:
<?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 dbConnection.php
<?php
require("config.php");
class dbConnection {
private $databaseConnection;
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: 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.请联系服务器管理员 webmaster@mvcportfolio.adambourg.com 并告知他们错误发生的时间,以及您所做的任何可能导致错误的事情。
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.此外,在尝试使用 ErrorDocument 处理请求时遇到 404 Not Found 错误。
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.基本上我正在尝试在 model 中完成我的所有数据库工作,然后通过 model 将其传递给 output 它的视图。
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
. 1 - 您正在对“config.php”使用require
,但您确实应该使用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"? 2 - 您是否在“config.php”中定义常量? The dbConnection::dbConnection()
function is looking for constants named dbHostName
, dbUserName
, dbPassword
, and dbDatabaseName
. dbConnection::dbConnection()
function 正在寻找名为dbHostName
、 dbUserName
、 dbPassword
和dbDatabaseName
的常量。 Make sure your "config.php" is defining constants.确保您的“config.php”正在定义常量。
3 - The while($row = mysql_fetch_array($dbMethods->fetchAllPortfolioItems()))
in dbFunctions.php
is wrong. 3 - dbFunctions.php 中的 while($row = mysql_fetch_array($ dbFunctions.php
while($row = mysql_fetch_array($dbMethods->fetchAllPortfolioItems()))
错误。 The mysql_fetch_array
portion of the while
is "recalculated" on every iteration which means that it's executed over and over again. while
的mysql_fetch_array
部分在每次迭代时都会“重新计算”,这意味着它会一遍又一遍地执行。 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.如果首先将$dbMethods->fetchAllPortfolioItems()
的值分配给变量,则 function 仅执行一次, while
将遍历结果。 Use this instead:改用这个:
$result = $dbMethods->fetchAllPortfolioItems();
while($row = mysql_fetch_array($result))
{
$pageContent = $row["description"];
}
As per the while
documentation:根据while
文档:
It tells PHP to execute the nested statement(s) repeatedly, as long as the while expression evaluates to TRUE.它告诉 PHP 重复执行嵌套语句,只要 while 表达式的计算结果为 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). mysql_fetch_array($dbMethods->fetchAllPortfolioItems())
部分, while
您正在使用将始终评估为TRUE
,只要查询返回一行,因为它被一遍又一遍地调用(因此每次返回相同的第一行) .
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
.导致“内部服务器错误”的错误可能是因为您的脚本占用的时间超过了php.ini
中允许的max_execution_time
。
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.本质上,您已经为dbConnection
制作了与 class 同名的 function,而不是使用__construct
方法。
You will see a very clear error message in your server error log, in apache on linux: /var/log/apache/error.log您将在服务器错误日志中看到一条非常清晰的错误消息,在 linux 上的 apache 中:/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/...我还将查看(如果从预览日志中不清楚)mysql 错误日志,该日志也在 /var/log/...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.