gpt4 book ai didi

php - 正在调用调用 MySQL 数据库的函数的 PHP 应用程序上的内部服务器错误

转载 作者:太空宇宙 更新时间:2023-11-03 11:14:03 25 4
gpt4 key购买 nike

基本上我正在构建一个 MVC 应用程序。在模型中,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...
}

}
?>

数据库连接.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);
}

}

?>

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");

?>

这是错误:

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.

基本上我试图在模型中完成我所有的数据库工作,然后通过模型将它传递给 View 以输出它。

最佳答案

我看到一些可能导致错误的问题:

1 - 您正在为“config.php”使用require,但您实际上应该使用require_once。无需多次加载。

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

2 - 您是否在“config.php”中定义常量? dbConnection::dbConnection() 函数正在查找名为 dbHostNamedbUserNamedbPassword 的常量>数据库名称。确保您的“config.php”正在定义常量。

3 - dbFunctions.php 中的 while($row = mysql_fetch_array($dbMethods->fetchAllPortfolioItems())) 是错误的。 whilemysql_fetch_array 部分在每次迭代时都会“重新计算”,这意味着它会一遍又一遍地执行。如果您首先将 $dbMethods->fetchAllPortfolioItems() 的值赋给一个变量,则该函数只执行一次,while 将遍历结果。改用这个:

$result = $dbMethods->fetchAllPortfolioItems();

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

根据 while文档:

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

您使用的 whilemysql_fetch_array($dbMethods->fetchAllPortfolioItems()) 部分将始终计算为 TRUE因为查询返回一行,因为它被一遍又一遍地调用(因此每次都返回相同的第一行)。

导致“内部服务器错误”的错误可能是因为您的脚本占用的时间超过 php.ini 中允许的 max_execution_time

关于php - 正在调用调用 MySQL 数据库的函数的 PHP 应用程序上的内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6567362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com