2

Im trying to move my database connection script to an external (and therefore more secure) file. However, it isnt working.

Here my PHP page (with include link)

<?php
include 'block/datalogin.php';
..etc

And heres block/datalogin.php

$dbhost = "localhost";
$dbuser = "************";
$dbpass = "***********";
$dbname = "************";
@mysql_connect($dbhost, $dbuser, $dbpass) or die("unable to
connect to database."
);
mysql_select_db($dbname) or die ("Unable to select");

Im sure the paths and login info are correct.

Any suggestions?

Jonah Katz
  • 5,230
  • 16
  • 67
  • 90

2 Answers2

2

Take out the "@" symbol in front of mysql_connect.

Paul Sonier
  • 38,903
  • 3
  • 77
  • 117
  • Still didnt work.. When I have the script copied and pasting directly into my original file (without include) it DOES work – Jonah Katz Aug 02 '11 at 19:50
1

First off, Don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.


<?php
    $dbhost = "localhost";
    $dbuser = "************";
    $dbpass = "***********";
    $dbname = "************";
    $conn=mysql_connect($dbhost, $dbuser, $dbpass) or die("unable to connect to database.");
    mysql_select_db($dbname) or die ("Unable to select");
?>

Then use the $conn variable in your query's

mysql_query("SELECT * from ....",$conn);
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • 1
    are you using the open & close tags `` put `error_reporting(E_ALL);` on the page you include to, it will give you some error you can work with then. – Lawrence Cherone Aug 02 '11 at 19:54
  • The `` did it! I originally left it out because i didnt think an include file would require this – Jonah Katz Aug 02 '11 at 19:57
  • good glad its working, yeah its required. also if its not parsed as php, some user could visit `block/datalogin.php` and see your logins... – Lawrence Cherone Aug 02 '11 at 19:59