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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:58:59+00:00 2026-06-16T18:58:59+00:00

I want to use a global variable and use bindValue() to assign it to

  • 0

I want to use a global variable and use bindValue() to assign it to a place holder so the value can be inserted into a database. The function I’m using is below

public function insertComment() {
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = 'INSERT INTO comments ( name, email, commentText, articleID ) VALUES ( :name, :email, :commentText, :articleID )';
    $st = $conn->prepare ( $sql );
    $st->bindValue( ":name", $this->name, PDO::PARAM_STR );
    $st->bindValue( ":email", $this->email, PDO::PARAM_STR );
    $st->bindValue( ":commentText", $this->commentText, PDO::PARAM_STR );
    $st->bindValue( ":articleID", $this->articleID, PDO::PARAM_INT );
    $st->execute();
    $conn = null;
}

The reason I can’t just make a public variable is because data is being posted from a form to it and using public or public static is an invalid syntax. The variables that I’m using are

$name = isset($_POST['name']) ? $_POST['name'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$commentText = isset($_POST['comment']) ? $_POST['comment'] : '';
$id = isset($_POST['id']) ? $_POST['id'] : '';

Is what I want to do even possible or am I better off finding another way to assign the values so I can insert into a database?

  • 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-16T18:59:00+00:00Added an answer on June 16, 2026 at 6:59 pm

    I would start by removing the creation of the database instance to outside the function, because by the looks of it you are opening and closing lots of database connections.

    class Foo
    {
        private $conn;
    
        public function __construct(PDO $conn)
        {
            $this->conn = $conn;
        }
    
        public function insertComment($name, $email, $comment, $articlId) {
            $sql = 'INSERT INTO comments ( name, email, commentText, articleID ) VALUES ( :name, :email, :commentText, :articleID )';
            $st = $this->conn->prepare ( $sql );
            $st->bindValue( ":name", $name, PDO::PARAM_STR );
            $st->bindValue( ":email", $email, PDO::PARAM_STR );
            $st->bindValue( ":commentText", $commentText, PDO::PARAM_STR );
            $st->bindValue( ":articleID", $articleID, PDO::PARAM_INT );
            $st->execute();
        }
    }
    
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $foo = new Foo($conn);
    $foo->insertComment($_POST['name'], $_POST['email'], $_POST['commentText'], $_POST['articleId']);
    

    Or perhaps even better have some request object and use that to inject into the method.

    Not sure what you mean by global variable though, because the request variables ($_GET, $_POST, etc) are superglobals meaning they are global by default. And can be accessed from anywhere (although it isn’t really best practice).

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

Sidebar

Related Questions

I want to assign global variable value within one function and use that variable
I am trying to use variable in different function, I want to set global
I have global variable. I want to use char data type so I can
I want to use a global variable for using as argument for two or
I am using xslt1.0.I want to use a global variable such a way that,
If you want to use global functions and variable dynamically you can use: window[functionName](window[varName]);
I want to use a global variable in inline assembly. asm( LDR R0,g_TsInitStackPointerAddress); Here
which is the cleanest way to use something like a global variable? Normally, using
I want use this 1 for using Bar code or QR code scanner. I
I want use BYTE_ORDER macro in my Xcode project but i can't because i

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.