php - Trouble connecting to mysql database when using a config file -
i can't connect database when use these files. here php code.
the db_config.php
file:
<?php define('db_user', "root"); define('db_password', "mypassword"); define('db_database', "mydbname"); define('db_server', "localhost"); ?>
the db_connect.php
file:
<?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(); } } ?>
the add_team.php
file:
<?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 teamdata (team_full_name, team_short_name, league) values ('$team_fn', '$team_sn', '$league')"); if($result) { $response["success"] = 1; $response["message"] = "team data 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) missing."; echo json_encode($response); } ?>
when use these files run add_team.php
file, program crashes (using in java). when change add_team.php
file not use db_connect
class this:
<?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 connect mysql"); $db = mysql_select_db("mydbname", $dbhandle); $result = mysql_query("insert teamdata (team_full_name, team_short_name, league) values ('$team_fn', '$team_sn', '$league')"); if($result) { $response["success"] = 1; $response["message"] = "team data 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) missing."; echo json_encode($response); } ?>
it works perfectly. following tutorial , version doesn't work how guy said set up. doing wrong? i'm new php excuse me if see statements deprecated, change them later. i'm using them because tutorial following pretty old , don't know how use non-deprecated alternatives yet.
also, config file setup db_connect
class, because don't have hard code database password in php files performing database transactions. seems bit safer version works. if can better version working, setup? if want make multiple inserts, updates, deletes, etc @ 1 moment, going have reconnect , disconnect every single time call 1 of php files performs database transaction?
please try following in 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(); } } ?>
and in add_team.php update
$db = new db_connect(); $dbcon = $db->connect(); $query = "insert teamdata (team_full_name, team_short_name, league) values ('{$team_fn}', '{$team_sn}', '{$league}')"; $result = mysql_query($query,$dbcon);
Comments
Post a Comment