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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:12:23+00:00 2026-06-04T13:12:23+00:00

Ok so i learning prepared statements in php because i hear they are a

  • 0

Ok so i learning prepared statements in php because i hear they are a lot safer that mysql

So now im just getting the hand of functions. I have made a function which connects to the mysql db and have it in a different folder that the page i want to use it on. So i want all my main functions in 1 file then i want to call them from the file on the pages i need them on. But now php does not like it.

Here is my functions.php page

function mysqlconnect(){

$host = 'localhost';
$port = 3306; // This is the default port for MySQL
$database = '';
$username = '';
$password = '';

// Construct the DSN, or "Data Source Name".  Really, it's just a fancy name
// for a string that says what type of server we're connecting to, and how
// to connect to it.  As long as the above is filled out, this line is all
// you need :)
$dsn = "mysql:host=$host;port=$port;dbname=$database";

// Connect!
$db = new PDO($dsn, $username, $password);
} 

And here is my test page just testing calling the function.

include 'functions/functions.php';

mysqlconnect();

$_POST['fish'] = "shadow" ;

$statement = $db->prepare("SELECT * FROM users WHERE username = ?");
$statement->execute(array($_POST['fish']));

while ($result = $statement->fetchObject()) {
    echo $result->username;
    echo "<br />";
}

Notice i include the file and call the function but im getting:

Notice: Undefined variable: db
Fatal error: Call to a member function prepare() on a non-objec

If i put the connect in the same php file has it everything works fine. But of course id like all the function in the same file and just call them when ever i need them. What am i doing wrong ??

  • 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-04T13:12:25+00:00Added an answer on June 4, 2026 at 1:12 pm

    $db is defined inside a function, so it cannot become global. It’s scope ends when the function ends.

    You should define $db outisde your function.

    A not-so-elegant solution:

    function mysqlconnect(){
      global $db; 
      $host = 'localhost';
      // etc.etc.
      $db = new PDO($dsn, $username, $password);
    }
    

    Please take note that using global, expecially in this context, is a really bad practice (it undermines code cleanliness, code reusability, and can lead to several other problems).

    A more elegant solution (like the other user says):

    function mysqlconnect(){
    
      $host = 'localhost';
      // etc.etc.
      $db = new PDO($dsn, $username, $password);
      return $db;
    }
    

    And then in your test page:

    $db = mysqlconnect();
    

    This is useful as you can use any variable name: makes your code more reusable in other scenarios.

    $donaldduck = mysqlconnect();
    

    would be ok too.

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

Sidebar

Related Questions

I'm currently learning PHP and MySQL. I'm just wrote a class that handles all
I am learning how to use prepared statements with php 5 mysqli objects and
I'm working on learning to use prepared statements with mysqli in PHP and usually,
I've just started learning aboud PDO and prepared statements (which sure seem to beat
Learning a lot in my few years of programming that the best projects are
When learning C# for the first time, I was astonished that they had no
I have been learning to use prepared and bound statements for my sql queries,
I'm learning Java and just started playing around with mysql and am having a
I am learning Extjs along with PHP and MySQL for one of my web
Prepared Statements Okay, I've just started taking a look at MySQLi prepared statements. This

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.