Jump to content

mysql connection file


]{ronic

Recommended Posts

Hi,

 

If I place the following into a file called db.php and save it into folder called includes using xampp:

 

$dbhost = 'localhost';$dbuser = 'root';$dbpasswd = '';$database = 'test'; $connection = mysql_connect("$dbhost","$dbuser","$dbpasswd") or die ("Couldn't connect to server."); $db = mysql_select_db("$database", $connection)or die("Couldn't select database.");

 
and try to access it by adding require_once 'includes/db.php'; into index.php, I get the following error:
 
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\index.php on line 28

 
But if I paste the code directly into index.php it works fine.. 
 
Both ways work fine using my paid hosting.. Whats going wrong?
 
Thanks
 
 
Link to comment
https://forums.phpfreaks.com/topic/288778-mysql-connection-file/
Share on other sites

This works:

 



$dbhost = 'localhost';
$dbuser = 'root';
$dbpasswd = '';
$database = 'test';


$connection = mysql_connect("$dbhost","$dbuser","$dbpasswd") 
or die ("Couldn't connect to server.");


$db = mysql_select_db("$database", $connection)
or die("Couldn't select database.");



$sql = mysql_query("SELECT firstname FROM clients ORDER BY firstname ASC");
while ($row = mysql_fetch_array($sql)) {

echo ''.$row['firstname'].'<br />';
}


 

This fails:

 



require_once 'includes/db.php';

$sql = mysql_query("SELECT firstname FROM clients ORDER BY firstname ASC");
while ($row = mysql_fetch_array($sql)) {

echo ''.$row['firstname'].'<br />';
}



 

with error:

 

 


Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\index.php on line 16


 

 

I can use require once on other files from the includes folder ok.. Seems to be only the db.php file that plays up.

 

dp.php:

 



<? 

$dbhost = 'localhost';
$dbuser = 'root';
$dbpasswd = '';
$database = 'test';


$connection = mysql_connect("$dbhost","$dbuser","$dbpasswd") 
or die ("Couldn't connect to server.");


$db = mysql_select_db("$database", $connection)
or die("Couldn't select database.");

?>


 

Strange issue, It only fails using xampp locally.. the basic script works fine on other professional servers as it should ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.