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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:51:36+00:00 2026-06-17T04:51:36+00:00

I use PHP’s PDO to connect to MySQL. I have this code for connection:

  • 0

I use PHP’s PDO to connect to MySQL. I have this code for connection:

$dbh = new PDO ( $db_host.$db_database, $db_user, $db_pass );
$dbh->exec ( "set names utf8" );

I have a function in another file:

function Image()
{
    include 'config/connect.php';
    #connected         

    $sql = 'Select * from settings where name="X" ';
    $stmt = $dbh->prepare($sql);
    $stmt->execute();
    $row = $stmt->fetchObject();
    $Template = $row->web_site_template;
    echo "Template"; 
}

I can use include connect.php file for that, but it’s not true.

I want use one function like connection() for connect to the mysql on all other functions, like:

function Image()
{
    connection();

    $sql = 'Select * from settings where name="X" ';
    $stmt = $dbh->prepare($sql);
    $stmt->execute();
    $row = $stmt->fetchObject();
    $Template = $row->web_site_template;
    echo "Template"; 
}
  • 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-17T04:51:37+00:00Added an answer on June 17, 2026 at 4:51 am

    I find a best solution for my question (How you can use a MYSQL connection for 1 or more functions):

    $db_database ="YourTableName";
    $db_user     ="YourUsername";
    $db_pass     ="YourPassword";
    $db_host     ="YourHostName";
    
    $dbh = new PDO ( "mysql:host=$db_host;dbname=$db_database", $db_user, $db_pass );
    $dbh->exec ( "set names utf8" );
    
    $database = $dbh; // Here you can use $dbh too, but I use $database to understand the difference here .
    
    #start function
    function Template(){
        global $database; //use $database as global
    
        $sql = 'Select * from YourTableName where column="Record"';
        $stmt = $database->prepare($sql); //use $database Instead $dbh 
        $stmt->execute();
        $row = $stmt->fetchObject();
        $Template = $row->web_site_template;
        echo $Template; 
       }
    
    Template(); // Here is your result.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Use PHP and MySQL. I have an array, P, which contains the value of
I use PHP to generate XML files. I have use some code below to
I use PHP and MySQL to manage the content on my server/site (I have
I'm trying to use PHP ActiveRecord in Codeigniter. I have a model like this:
use PHP and MySQL. Want my website to have the feature of image uploading
Use php and MySQL, I want to get the duration in php code between
I use PHP. I have an URL that looks like this http://www.mydomain.com/mydir/mydir2/?something=hello I want
I use PHP, MySQL and Zend Framework. I have some simple tables with simple
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Use PHP and Mysql. Fields in my table (product) are id , full_price ,

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.