使用配置文件

时间:2016-04-25 11:07:58

标签: php mysql database

当我使用这些文件时,我无法连接到我的数据库。这里是php代码。

db_config.php文件:

<?php
    define('DB_USER', "root");
    define('DB_PASSWORD', "mypassword");
    define('DB_DATABASE', "mydbname");
    define('DB_SERVER', "localhost");
?>

db_connect.php文件:

<?php

    class DB_CONNECT
    {
        //constructor
        function __construct()
        {
            $this->connect();
        }

        //destructor
        function __destruct()
        {
            $this.close();
        }

        function connect()
        {
            // import database connection variables
            require_once __DIR__ . '/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());

            // returing connection cursor
            return $con;
        }

        function close()
        {
            //closing db connection
            mysql_close();
        }
    }

?>

add_team.php文件:

<?php

$response = array();

if(isset($_POST['team_full_name']) && isset($_POST['team_short_name']) && isset($_POST['league']))
{
    $team_fn = $_POST['team_full_name'];
    $team_sn = $_POST['team_short_name'];
    $league = $_POST['league'];

    require_once __DIR__ . '/db_connect.php';
    $db = new DB_CONNECT();

    $result = mysql_query("INSERT INTO teamData (team_full_name, team_short_name, league) VALUES ('$team_fn', '$team_sn', '$league')");

    if($result)
    {
        $response["success"] = 1;
        $response["message"] = "Team Data successfully added.";

        echo json_encode($response);
    }
    else
    {
        $response["success"] = 0;
        $response["message"] = "Error occurred!";

        echo json_encode($response);
    }
    //for testing
    //mysql_close($dbhandle);
}
else
{
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing.";
    echo json_encode($response);
}

?>

当我使用这些文件来运行add_team.php文件时,我的程序崩溃了(在Java中使用它)。但是,当我将add_team.php文件更改为不使用DB_CONNECT类时,如下所示:

<?php

$response = array();

if(isset($_POST['team_full_name']) && isset($_POST['team_short_name']) && isset($_POST['league']))
{
    $team_fn = $_POST['team_full_name'];
    $team_sn = $_POST['team_short_name'];
    $league = $_POST['league'];

    $dbhandle = mysql_connect("localhost", "root", "mypassword") or die ("unable to connect to MySQL");
    $db = mysql_select_db("mydbname", $dbhandle);

    $result = mysql_query("INSERT INTO teamData (team_full_name, team_short_name, league) VALUES ('$team_fn', '$team_sn', '$league')");

    if($result)
    {
        $response["success"] = 1;
        $response["message"] = "Team Data successfully added.";

        echo json_encode($response);
    }
    else
    {
        $response["success"] = 0;
        $response["message"] = "Error occurred!";

        echo json_encode($response);
    }

    mysql_close($dbhandle);
}
else
{
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing.";
    echo json_encode($response);
}

?>

它完美无缺。我正在学习一个教程,那个不起作用的版本是这个家伙如何设置的。我究竟做错了什么?我是php的新手,请原谅我,如果你看到被弃用的语句,我会在以后更改它们。我使用它们是因为我所关注的教程已经很老了,而且我还不知道如何使用未弃用的替代品。

另外,我喜欢使用DB_CONNECT类配置文件,因为我不必在执行数据库事务的php文件中硬编码我的数据库密码。它似乎比有效的版本更安全。因此,如果我能够使用更好的版本,这是一个很好的设置吗?如果我想在一个时刻进行多次插入,更新,删除等,如果每次调用执行数据库事务的php文件,我将不得不重新连接和断开连接?

2 个答案:

答案 0 :(得分:0)

请在db_connect.php中尝试以下

<?php

class DB_CONNECT
{
    function connect()
    {
        // import database connection variables
        require_once __DIR__ . '/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());

        // returing connection cursor
        return $con;
    }

    function close()
    {
        //closing db connection
        mysql_close();
    }
}

?>

在你的add_team.php更新中

$db = new DB_CONNECT();
$dbCon = $db->connect();
$query = "INSERT INTO teamData (team_full_name, team_short_name, league) VALUES ('{$team_fn}', '{$team_sn}', '{$league}')";
$result = mysql_query($query,$dbCon);

答案 1 :(得分:0)

想出来!愚蠢的小虫子花了我几个小时修复。在DB_CONNECT课程的析构函数中,我调用了$this.close()而不是this->close()。完全修复它。