Jump to content

Running pdo in seperate file


shaung

Recommended Posts

Hello,

 

I keep getting a 'Fatal error: Call to a member function prepare() on a non-object' error.  My goal is to have a function where I can pass in sql statements and get back the $row and $arrValues.   I have included the function file in the file that I make the call in and the include works.   

 

Is it possible to do something like this in a separate file, passing in a sql statement?

function runStatement($dbh, $sql){
            $stmt = $dbh->prepare($sql);
            $stmt->execute();
            return $stmt;
}

and use it something like this 

 

        $sql = "SELECT * FROM databaseTable";

        $stmt = runStatement($dbh, $sql);
        $arrValues = $stmt->fetchAll(PDO::FETCH_ASSOC);
         $row = $stmt->fetch();

Then I could run $arrValues and $row in a loop to show their values as needed.

I have been trying figure this out for two days now.

 

My end goal is if bool = true, run sql statement A, else run sql statement B.  

 

Anyone?

Edited by shaung
Link to comment
Share on other sites

Where did you define $dbh? But, really, why are you even using prepare() at all in this instance? Prepared statements are meant for using placeholders in the query that will be 'replaced' when the query is executed. Since you aren't using placeholders the prepare statement is a waste of code.

Link to comment
Share on other sites

$dbh is defined in a file that is included in the page.  It works perfectly within the page as seen below.  

 

 $sql = "SELECT * FROM fac_detail INNER JOIN institution ON fac_detail.institution_id = institution.id " . "INNER JOIN degree ON fac_detail.degree_id = degree.id";


$stmt = $dbh->prepare($sql);
$stmt->execute();
$arrValues = $stmt->fetchAll(PDO::FETCH_ASSOC);
$row = $stmt->fetch();

I then show the returned data in a datatable, works great.  The datatable also contains two dropdown lists that I need to fill with database calls to two other tables.  I need to run three sql statements to completely fill the table.  I wish to reuse code if possible.

 

Right now Im trying to figure out the logic flow to fill the table and dropdowns.

 

If user clicks edit button associated with a row, row becomes editable and dropdowns fill with choosable options.

 

I want to make calls  to a function that returns the data to put in the dropdowns.  I have tried running the pdo code using if statements but it doesn't work.  It seems to go out of scope.  

 

Is it possible to have a function that I can pass sql statements to that returns the $row and $arrValues?

 

 

 

 

 

 

Link to comment
Share on other sites

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.