php - PDO not working within function -


I am trying to create a function to draw the content of a page using a PDO Prepayer statement from a MySQL table. I

My code works fine outside of the function I define but no matter what I do it will not work within the function - I get the following error:

< Strong> Critical Error: Call Member Function

function getPageContent ($ page) {$ st = $ db- & gt; Prepare ("Choose the title from where to select the content?"); $ St- & gt; Execute (array ($ page)); $ PageContent = $ st- & gt; Fetch (); $ Text = wordwrap ($ page content ['content'], 100, "\ n"); $ Tab = 4; $ Text = str_repeat (chr (9), $ tab) Str_replace (chr (10), chr (10). Str_repeat (chr (9), $ tabs), $ text); Repeat $ text; }

and then

  & lt; Php getPageContent (main) ;? & Gt; I have tried to use a query instead of making statements, just calling getPageContent () and I get a single error.  

Thank you!

You are trying to access variable $ db Is outside

Reboot your database within the function in your case - $ db = new PDO .... , or - perhaps better and easier - Import global variables :

  function getPageContent ($ page) {global $ db;  

Where and how to discuss the best database database, if you want to be involved, there is a place to start (many Others are also on the SO). But if you are just coming to PHP, then I would say it is ok to use the global variable.


Comments