Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Having difficulty with pulling connection info from .inc file

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Ok, I'm still new to this, so I'm looking for constructive help here. ;-) I'm having some problems getting my connection information to work from an .inc file that is called up by a .php3 file. For example, initially, I had hardcoded the connection information, setting the start.php3 file like this:

<?
$db_name = &quot;my_db&quot;;
$table_name = &quot;products&quot;;
$connection = @mysql_connect(&quot;localhost&quot;, &quot;bob&quot;, &quot;x8di32&quot;) or die(&quot;Sorry, can't connect&quot;);

$db = @mysql_select_db($db_name, $connection) or die(&quot;sorry, couldn't select database&quot;);
$sql = &quot;
SELECT prod_id, distributor, cost, year
FROM $table_name
WHERE year > '$lowyear'
ORDER BY year
&quot;;
$result = @mysql_query($sql, $connection) or die(&quot;sorry, couldn't execute query.&quot;);
...

This worked just fine. HOWEVER, I'm concerned about having the database, username, and password hardcoded in this file... (Should I be concerned about this if the file can't be viewed through 'source' in the browser or saved?)

SO THEN... I tried to put the password/login info in one file (stuff.inc) and the rest in the main file (start.php3)... doing something like this:

======================start.php3
<?php
require('stuff.inc');
?>
<?
$table_name = &quot;products&quot;;

$db = @mysql_select_db($db_name, $connection) or die(&quot;sorry, couldn't select database&quot;);
$sql = &quot;
SELECT prod_id, distributor, cost, year
FROM $table_name
WHERE year > '$lowyear'
ORDER BY year
&quot;;
$result = @mysql_query($sql, $connection) or die(&quot;sorry, couldn't execute query.&quot;);
...

======================stuff.inc
<?php
$dbname = 'my_db;
$hostname = 'localhost';
$username = 'bob';
$password = 'x8di32';

$connection = @mysql_connect($hostname, $username, $password) or die(&quot;Sorry, can't connect&quot;);
?>

THE PROBLEM IS... When I run this, I get the 'sorry, couldn't execute query' message.

Any suggestions?

Thanks!
 
Try this. Seems to work fine:

>><?php
>> $dbname = 'my_db;
>> $hostname = 'localhost';
>> $username = 'bob';
>> $password = 'x8di32';
>>
>>$connection = @mysql_connect($hostname, $username, >>$password) or die(&quot;Sorry, can't connect&quot;);
>>?>

Code:
<?php
  $hostname = 'localhost';
  $username = 'bob';
  $password = 'x8di32';

$connection = @mysql_pconnect($hostname, $username, $password) or die(&quot;Sorry, can't connect&quot;);
?>

>><?php
>>require('stuff.inc');
>>?>
>><?
>>$table_name = &quot;products&quot;;
>>
>>$db = @mysql_select_db($db_name, $connection) or die(&quot;sorry, couldn't select database&quot;);
>>$sql = &quot;
>>SELECT prod_id, distributor, cost, year
>>FROM $table_name
>>WHERE year > '$lowyear'
>>ORDER BY year
>>&quot;;
>>$result = @mysql_query($sql, $connection) or die(&quot;sorry, couldn't execute query.&quot;);
>>...

Code:
<?php

include_once(&quot;stuff.inc&quot;);

$db_name = &quot;db_name&quot;;
$table_name = &quot;products&quot;;

/*
  don't need to assign your database selection to a variable.
*/
@mysql_select_db($db_name, $connection) or die(&quot;sorry, couldn't select database&quot;);

$sql = &quot;SELECT prod_id, distributor, cost, year
        FROM $table_name
        WHERE  year > '$lowyear'
        ORDER BY year&quot;;

/*
  you don't need to add the connection pointer to your query as long as this is the only database connection you have open
*/

$result = @mysql_query($sql) or die(&quot;sorry, couldn't execute query.&quot;);

...

Chad. ICQ: 54380631
 
Also, avoid putting your username and password in a file called .inc as a file with this ending will not get parsed as php and will be presented to the browser. This means that if you have a file config.inc then I could go to your site:


and the chances are that I would be able to read the contents of the file. Call it something like config.php3 so if I went to the file directly the php would be parsed out before I got to see the information.

Will.
fortytwo
will@hellacool.co.uk
 
Very true!

If your site's .inc files are PHP ONLY, you can make all .inc be parsed as PHP scripts (in your apache config files or in .htaccess if you can't modify your config file) by way of
Code:
Add Type Application/x-httpd-php .inc
and so forth.

Also, it is probably good practice anyway to append a .php to your .inc files (such as
Code:
config.inc
to
Code:
config.inc.php
). This is a newer practice but has become standardized with the newer theories and approaches to development.

Chad. ICQ: 54380631
 
Thanks! I finally have it working. ;-) Also, I appreciate the info about the inc vs. php issue. You've helped clarify some things.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top