Jump to content

mysql connection file


Go to solution Solved by jazzman1,

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 ;)

Edited by ]{ronic
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.