当我使用这些文件时,我无法连接到我的数据库。在这里,他们带有 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 文件之一时,我是否都必须重新连接和断开连接?
请尝试以下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);
想通了!愚蠢的小错误,花了我几个小时才修复。在DB_CONNECT
类的析构函数中,我调用的是$this.close()
而不是this->close()
。完全修复了它。