Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8754419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:36:58+00:00 2026-06-13T13:36:58+00:00

I’m trying to offload the process of connecting to a database to a DBConfig.php

  • 0

I’m trying to offload the process of connecting to a database to a DBConfig.php file, as suggested pretty much everywhere, because I’ll need to reuse the code in several files.

The suggestion is always framed as (edited to reflect my actual code):

-------DBConfig.php-------
<?php
$link = mysql_connect('localhost','user','pass');
if (!$link) {
  die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('db_name');
if (!$db_selected) {
    die ('Can\'t use db_name : ' . mysql_error());
}
?>

-------Parent.php-------
<!DOCTYPE html>
<?php
require_once 'DBConfig.php';
$something = mysql_query("SELECT * FROM `table` LIMIT 0, 30 ");
// This query works fine if the mysql_connect() and
// mysql_select_db() stuff is in this script in place
// of the require_once.
$listofthings = array();
while($temp = mysql_fetch_array($something)){
    $listofthings[] = $temp;
}
// Do other things too
?>

But when I try to do mysql_fetch_array($something), it fails with the warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given.


Keeping in mind that everything works beautifully if I simply drop the contents of DBConfig.php into the parent script instead of require 'DBConfig.php';…

And also noting that print(require 'DBConfig.php'); //- 1 (PHP is returning a 1 to indicate that it is successfully locating and including the file. See example 5)…

What is going on, and how do I fix it? I am using the default configuration for WAMPServer (Apache 2.4.2, PHP 5.4.3), running on Windows 7 x64.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:36:59+00:00Added an answer on June 13, 2026 at 1:36 pm

    If this

    require 'DBConfig.php';
    

    is literally what you are doing, there is absolutely no way the script can be executed in some other scope. If you were using a http:// URL, it would be the cause but not if you use a relative path like you do.

    You are not doing any error checking after connecting to your database, nor after the mysql_query() call, so it’s more likely it’s simply the query breaking. Add proper error checking to your queries and you will know more.

    Another possibility is if you call DBConfig.php inside a function. Functions will mess with your include’s scope. But there’s none in your example, so I’m assuming that is not the issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm trying to create an if statement in PHP that prevents a single post
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.