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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:55:26+00:00 2026-06-14T14:55:26+00:00

I am beginning to convert all old mysql_ functions to PDO and have a

  • 0

I am beginning to convert all old mysql_ functions to PDO and have a little trouble grasping this concept. I want to be able to access my PDO connection variable throughout my function pages so that it can be called as needed without making a connection script at the beginning of each function. I’ll try to explain my setup as best as possible.

This is my init.php page that loads all required php pages. This page is included at the top of each of my pages, but not function pages.

init.php

<?php
session_start();

error_reporting(E_All);

require 'database/connect.php';
require 'functions/users.php';
require 'functions/general.php';
require 'functions/trainer.php';
?>

Here is my connect file that opens my db connection. Yes I have a mysql_connect open as well because I can not disable all my other functions while converting all the others over to PDO.

connect.php

<?php 

$connect_error = 'Sorry there is a problem with the database connection.';
mysql_connect('Localhost', 'customn7', 'I<3deadlifts!') or die($connect_error);
mysql_select_db('customn7_cm') or die($connect_error) or die($connect_error);
?>

<?php   

//PDO database connect
$config['db'] = array(
    'host'      => 'Localhost',
    'username'  => 'customn7',
    'password'  => '********',
    'dbname'    => 'customn7_cm'
);

try {
$db = new PDO('mysql:host=' .$config['db']['host']. ';dbname=' .$config['db']['dbname'], $config['db']['username'], $config['db']['password']);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec("SET CHARACTER SET utf8");
} 

catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}

?>

Here is my functions/trainer.php where the function is written:

trainer.php

function exist_client_to_class($cd){
list($user_id, $class_id, $first_name, $last_name, $nickname) = explode('|', $cd);

try{

$stmt = $db->prepare('INSERT INTO clients 
(`user_id`, class_id, first_name, last_name, nickname, date)
VALUES (:user_id, :class_id, :first_name, :last_name, :nickname, CURDATE())
');

$stmt->execute(array(
        ':user_id' => $user_id, 
        ':class_id' => $class_id, 
        ':first_name' => $first_name,
        ':last_name' => $last_name,
        ':nickname' => $nickname)
        );
}

catch(PDOException $e) {
    echo 'Error: ' . $e->getMessage();
}

}

As of now I can not access my $db variable from my trainers.php page. Can someone help me with this?

UPDATE

Here is the code that calls the function:

// Post Selected name to current class.
if (isset($_POST['exist_to_class'])){
if (empty($_POST['client_data']) === true){
    $errors [] = 'You much select a client to be added to the class.';
} else {
    if (isset($_POST['client_data']) && !empty($_POST['client_data']));
    foreach ($_POST['client_data'] as $cd){
     exist_client_to_class($db, $cd);
     header('Location: view_class.php?class_id='.$class_id.' ');
    } // foreach $cd
} // else

} //isset
  • 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-14T14:55:27+00:00Added an answer on June 14, 2026 at 2:55 pm

    First off, I strongly recommend you don’t listen to anyone telling you to set it as global.


    Pass the connection object to the function, using an argument:

    exist_client_to_class(PDO $pdo, $cd) { //...
    

    That’s a simplified version of what’s called Dependency Injection.

    A more robust version includes by wrapping your functions into classes (and instantiating objects from those classes), and passing the PDO object (or some sort of Factory object) to the constructor of that object. Then it would be “globally” available throughout the methods (functions) of that object.

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

Sidebar

Related Questions

I have a project, at the beginning of this project we choose Hibernate instead
I want to be able to expand or collapse all children of a particular
How can I convert this script into a MATLAB function? clear all; set={AB02XY_1,ZT99ER_1,UI87GP_1}; fileData1
I have a string in which all of the beginning of every word are
Since beginning to use VB.NET some years ago I have become slowly familiar with
I was beginning to write a bubble sort for this when I thought maybe
This is the beginning of a script that displays thumbnails, worked fine, but I've
Basically I want to be able to grab the ending of an url, and
I have a table with a row that looks like this: ( 20091231 48498429,
This question is related to this other question @ SuperUser . I want to

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.