【问题标题】:PHP Fatal error: require_once(): Failed opening requiredPHP 致命错误:require_once():需要打开失败
【发布时间】:2014-05-09 08:13:15
【问题描述】:

更新 谢谢您解决了问题,但现在我收到此错误“连接尝试失败,因为连接方在一段时间后没有正确响应或建立连接失败,因为连接的主机没有响应。”我关闭了防火墙并我没有任何杀毒软件

我正在按照本教程将 android 应用程序连接到数据库 http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/

我在 WAMPSERVER PHP 版本 5.4.3 中托管我的 php 文件我收到这两个错误 我已经尝试修复这个错误好几个小时了,但没有运气,请你帮我修复它

" 警告:require_once(C:\wamp\wwwC:\wamp\www\db_connect.php):无法打开流:C:\wamp\www\get_property_details.php 第 12 行中的参数无效"

还有这个错误

“致命错误:require_once(): 无法在 C 中打开所需的 'C:\wamp\wwwC:\wamp\www\db_connect.php' (include_path='.;C:\php\pear') :\wamp\www\get_property_details.php 第 12 行"

这些是我的文件 db_config.php

define('DB_USER', "student"); 
define('DB_PASSWORD', "student2"); 
define('DB_DATABASE', "test"); 
define('DB_SERVER', "student.database.university.ac.uk"); 
?>

db_connect.php

<?php


class DB_CONNECT {

    function __construct() {

        $this->connect();
    }


    function __destruct() {

        $this->close();
    }


    function connect() {

       require_once __DIR__ . "C:\\wamp\\www\\db_config.php";

        $con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());

        $db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());

        return $con;
    }


    function close() {

        mysql_close();
    }

}

?>

get_student_details.php

<?php

/*
 * Following code will get single student details
 * A student is identified by student id (student_id)
 */

// array for JSON response
$response = array();

// include db connect class
require_once __DIR__ . "C:\\wamp\\www\\db_connect.php";

// connecting to db
$db = new DB_CONNECT();

// check for post data
if (isset($_GET["student_id"])) {
    $student_id = $_GET['student_id'];

    // get a student from students table
    $result = mysql_query("SELECT * FROM TABLE_STUDENT WHERE student_id = $student_id");

    if (!empty($result)) {
        // check for empty result
        if (mysql_num_rows($result) > 0) {

            $result = mysql_fetch_array($result);

            $student = array();
            $student["student_id"] = $result["student_id"];
            $student["student_fname"] = $result["student_fname"];
            $student["student_lastName"] = $result["student_lastName"];
            $student["address"] = $result["address"];
            $student["postcode"] = $result["postcode"];
            $student["grade"] = $result["grade"];

            // success
            $response["success"] = 1;

            // user node
            $response["student"] = array();

            array_push($response["student"], $student);

            // echoing JSON response
            echo json_encode($response);
        } else {
            // no student found
            $response["success"] = 0;
            $response["message"] = "No student found";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no student found
        $response["success"] = 0;
        $response["message"] = "No student found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

【问题讨论】:

  • 快速注意,您应该远离 mysql 并使用 mysqli,因为 mysql 已被弃用并将被删除

标签: php android mysql


【解决方案1】:
require_once __DIR__ . "C:\\wamp\\www\\db_config.php";

上面这行是你的问题。你的路径是错误的,因为你告诉 PHP 该文件位于当前目录和名为“C:\wamp\www\db_config.php”的路径中,这显然是不正确的。

您的修复可能是删除__DIR__,这是不必要的,因为您似乎已经拥有文件的完整路径:

require_once "C:\\wamp\\www\\db_config.php"

【讨论】:

  • 最好使用文件的相对路径而不是绝对路径。
  • 我会说最好使用文件的动态完整路径。
  • 谢谢您解决了问题,但现在我收到此错误“连接尝试失败,因为连接方在一段时间后没有正确响应或建立连接失败,因为连接的主机没有响应. " 我关闭了防火墙,没有安装任何杀毒软件
猜你喜欢
  • 2016-07-06
  • 2014-10-21
  • 2017-07-19
  • 2011-08-04
  • 1970-01-01
  • 2020-03-02
相关资源
最近更新 更多