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 8942059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:26:23+00:00 2026-06-15T11:26:23+00:00

In my opinion i have a weird problem. The part below works fine $sql

  • 0

In my opinion i have a weird problem. The part below works fine


$sql = "
    UPDATE 
        ".$table[0]."
    SET 
        p_title = '".$_POST['p_title']."', 
        p_date = '".$_POST['p_date']."' 
    WHERE 
        p_id = '".$_POST['p_id']."'
";
if(!$db->exec($sql)){
    echo($defaults->throwError('MySql error',$sql,implode(":",$db->errorInfo())));
}else{
    $defaults->writeLog($table,$db->lastInsertId(),'update');
}

But when i try the code below i get an ‘Fatal error: Call to a member function exec() on a non-object in’


class Defaults{
    [..]
    public function query($sql){
        if(!$db->exec($sql)){
            echo($defaults->throwError('MySql error',$sql,implode(":",$db->errorInfo())));
        }else{
            $defaults->writeLog($table,$db->lastInsertId(),'update');
        }
    }
    [..]
}

and then on my page


$defaults = new Defaults();
$defaults->query("
    UPDATE 
        ".$table[0]."
    SET 
        p_title = '".$_POST['p_title']."', 
        p_date = '".$_POST['p_date']."' 
    WHERE 
        p_id = '".$_POST['p_id']."'
");

How come?

Ah, it was about the $db in the class. However, when i do something like


public function query($sql){
    $db = new PDO($dbdata->hostname,$dbdata->username,$dbdata->password);
    if(!$db->exec($sql)){
        echo($defaults->throwError('MySql error',$sql,implode(":",$db->errorInfo())));
    }else{
        $defaults->writeLog($table,$db->lastInsertId(),'update');
    }
}

I get a nasty

Fatal error: Uncaught exception 'PDOException' 
with message 'invalid data source name' 
in /class.defaults.php:8 

Stack trace: 
#0 /class.defaults.php(8): PDO->__construct('', NULL, NULL) 
#1 /class.form.php(269): Defaults->query('?????????UPDATE...') 
#2 /module.projectbeheer.edit.php(25): Form->proceed('update', 'p_id', 'rows', Array, Array, '') 
#3 /class.content.php(16): include_once('/path/') 
#4 /administratie.php(72): Content->write('/BraamsArchief/...') 
#5 {main} thrown in /class.defaults.php on line 8
  • 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-15T11:26:24+00:00Added an answer on June 15, 2026 at 11:26 am

    You must provide $db either as a global or as a class property. Otherwise, you cannot access it in the method scope.

    So either:

    public function query($sql) {
        global $db;
        // now you can use $db as you would in global scope
    }
    

    or else you have to set a class property (e.g. $this->db) with your database connection object and access it using $this->db in your method.

    Note that if you use the global solution, you will need to ensure that the variable is always called $db when it is initially defined. Otherwise, your method may not be able to find it and you will get the same error.

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

Sidebar

Related Questions

I have a weird problem (at least in my opinion). When I add a
I have a little experience with WCF and would like to get your opinion/suggestion
In my opinion, Vimscript does not have a lot of features for manipulating strings.
I know this is largely an opinion, but I'm interested if you have one
i would like to have your opinion in a project i am currently working
Does XHTML have an 'opinion' regarding the use of <thead> , <tfoot> and <tbody>
I would like to have your opinion about the subject version control, but focusing
A lot of people seem to have an opinion about which is better. I'm
did someone tried this code library and have opinion about it? http://code.msdn.microsoft.com/exprserialization 10x
I have this weird thing happening: when clicking spans in the navigation the content

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.