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

  • SEARCH
  • Home
  • 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 6142597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:24:35+00:00 2026-05-23T18:24:35+00:00

I am having a problem when trying to access some functions in my mysql

  • 0

I am having a problem when trying to access some functions in my mysql class. The function I have to connect to my database is as follows:

function connect_DB() {
    $this->connection = mysql_connect($this->dbURL, $this->dbUser, $this->dbPass) or trigger_error('Connection failed: ' . mysql_error($connection), E_USER_ERROR);
    $this->db = mysql_select_db($this->dbName, $this->connection) or trigger_error('Database selection failed: ' . mysql_error($db), E_USER_ERROR);
}

And I create my mysql object using this:

$dbConf = new StoreConfDB();
$dbConf->connect_DB();

Now I have a function that is supposed to grab all of the column names from a specified table. Code:

function get_db_columns($table) {
global $dbConf;
print_r($dbConf->get_db());
$result = $dbConf->query_DB_resource('SELECT * FROM ' . $table);
for($i = 0; $i < mysql_num_fields($result); $i++) {
    $meta = mysql_fetch_field($result, $i);
    $fields[] = $meta->name;
}
mysql_free_result($result);
return $fields;
}

But I am getting errors, such as:

Warning: mysql_query(): 5 is not a valid MySQL-Link resource

And if I try to remove the connection argument from the mysql_query call I get this:

Notice: Could not run query: No database selected in ...

Now I am running a debugger and have pinpointed what the problem is (I think). When the class works, the connection variable is set to: resource id='5' type='mysql link'

But when it calls the query function in the mysql class from inside the get_db_columns() function the connection variable is: resource id='5' type='Unknown'

So somehow the connection variable gets messed up even though both of the connection variables should be the same thing? (from $dbConf)? I’ve tested this function in 2 different places, it works in one and not the other! Please help!

@footy:

A print_r on $dbConf returns:

StoreConfDB Object ( [dbURL] => localhost [dbUser] => root [dbPass] => [dbName] => db1 [connection] => Resource id #5 [db] => 1 )

The query_DB_resource function:

function query_DB_resource($query) {
    $sql_query = mysql_query($query) or trigger_error('Could not run query: '. mysql_error());
    return $sql_query;
}
  • 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-05-23T18:24:36+00:00Added an answer on May 23, 2026 at 6:24 pm

    I noticed a couple of issues:

    You have mysql_error($connection) and mysql_error($db). Those aren’t valid parameters. You should be using $this->connection in both cases. That will give you better error output.

    Normally, it doesn’t terribly matter if you pass in a connection resource to mysql_query, but in this case you may want to: mysql_query($query, $this->connection). While I can’t say that that is a guaranteed fix, it is likely to help.

    Oh, and BTW — every time someone uses global God kills a kitten. It’s true. He told me. Either that or I had some bad nachos. (Yes, I’m a zealot on this issue. It’s a good thing. trust me)

    Regardless, if you need a variable inside of a function either pass it in, or make a property of a new class (make the StoreConfDB a Singleton? Make it accessible by factory method? You have a number of choices) — avoiding global as a habit removes something nasty called unexpected side effects.

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

Sidebar

Related Questions

I have been trying to tackle this problem , but I am having difficulty
Having a problem trying to create a function, as part of a BizTalk helper
I am trying to convert this test code to C# and having a problem
I'm trying to do some basic paging in MSSQL. The problem I'm having is
I have been having some problems trying to get my PHP running. When I
I'm having a problem using the java.text.MessageFormat object. I'm trying to create SQL insert
I'm trying to find an answer for a problem my developers are having. I
Ok, so here's the full description of the problem I'm having: I am trying
I am having problems connecting to a Sqlite database through System.Data.Sqlite. I was trying
I'm trying to add some functionality from a plugin I have made into a

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.