1,105,221 Community Members

PHP file included but code not working

Member Avatar
rajesh1158
Junior Poster in Training
59 posts since Oct 2010
Reputation Points: -6 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I am trying to connect mysql from within PHP function. The credentials are stored in a separate PHP file, which I am including it inside the function. But when I run the script, I am getting following error:

Warning: mysql_query(): A link to the server could not be established in C:\xamp
p\htdocs\abc\test.php on line 12. Access denied for user 'ODBC'@'localhost' (using password: NO)


private function insertToMysql()
{
    include_once('connect_db.php');

    $connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die('Oops connection error -> ' . mysql_error());
    mysql_select_db(DB_DATABASE, $connection) or die('Database error -> ' . mysql_error());

    //Queries

}

I tried echoing the include_once and it returned 1. This means, the connect_db.php is included, but somehow the constants are not loaded. What could be the issue??? The credentials are perfect and I verified them. I am not a newbie to PHP.

Member Avatar
phorce
Veteran Poster
1,005 posts since Jul 2011
Reputation Points: 105 [?]
Q&As Helped to Solve: 133 [?]
Skill Endorsements: 29 [?]
Featured
 
0
 

Hey,

I don't think you can do this.. I'm guessing your database information is in the file, so therefore, the connection code needs to be there as well.

$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die('Oops connection error -> ' . mysql_error());
    mysql_select_db(DB_DATABASE, $connection) or die('Database error -> ' . mysql_error());

if these details are correct, you can then include/require/require_once the file and a connection to the database will be established WHICH will enable you to execute queries.. It doesn't know what DB_SERVER etc is/where it's stored.

EDIT: Can you post what is stored inside "connect_db", removing passwords etc.. =) Also, it's not a good idea to use mysql_* anymore as it's being depreciated.. Use mysqli_* etc..

Blastcore
Newbie Poster
2 posts since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

Just saying, that the use of mysql_ is getting deprecated. You could use PDO_MySQL or MySQLi

Question Answered as of 1 Year Ago by phorce and Blastcore
Member Avatar
rajesh1158
Junior Poster in Training
59 posts since Oct 2010
Reputation Points: -6 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks phorce!!

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: