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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:20:43+00:00 2026-06-07T02:20:43+00:00

New to PDO. Here is my function. We are connected to the database and

  • 0

New to PDO. Here is my function. We are connected to the database and everything is working but this function when called is killing off the rest of PHP to be executed. Obviously something is wrong with it.

$dbh = new PDO("mysql:host=$host;dbname=$database", $username, $password);
        global $dbh;

        /*** set the error reporting attribute ***/
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

function selectall($table){
            $stmt = $dbh->prepare("SELECT * FROM :table");
            $stmt->bindParam(':table', $table, PDO::PARAM_STR,20);
            $stmt->execute();
            $result = $stmt->fetchAll();
            return $result;
        }

Called on a page like this:

<?php $telephones = selectall('telephone'); foreach($telephones as $telephones) { echo $telephone['title'].', '; } ?>

/// EDIT – I have tried all of your methods and the function is still breaking when called on the page. Here is the entire code. Slightly modified for testing purposes.

try { 

        $dbh = new PDO("mysql:host=$host;dbname=$database", $username, $password);

        /*** set the error reporting attribute ***/
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $sql = "SELECT * FROM system";
        foreach ($dbh->query($sql) as $row)
            {
            $url = $row['url'];
            $election = $row['election'];
            $election_date = $row['election_date'];
            $sitename = $row['sitename'];
            }


        //FUNCTION

        function selectall($table){
            global $dbh;
             $sql = "SELECT * FROM telephone";
            foreach ($dbh->query($sql) as $row){
                print $row['title'] .' - '. $row['name'] . '<br />';
            }
        }


        /** close database connections **/
        $dbh = null;

    }

    catch(PDOException $e) { 

        echo $e->getMessage(); 

    }

The code outside the function ie. the one reflecting the table system is working perfectly Yet the one inside the function when called as a function later is killing off everything after it is called and is not executing.

  • 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-07T02:20:45+00:00Added an answer on June 7, 2026 at 2:20 am

    Your second line generates a fatal error, when you’re trying to use an undefined variable ($dbh) as an object.

    You need to import it from the global scope like this:

    global $dbh;
    

    or pass it as a parameter to the function.

    After you fix this, your code still won’t work, as you cannot use parameters for identifiers (table or column names, or even in limit and offset). Unfortunately you have to resort to string concatenation there.

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

Sidebar

Related Questions

Why doesn't this PHP PDO snippet work? $DBH = new PDO(mysql:host=$host;dbname=$dbname, $user, $pass); $STH->bindParam(':fftb',
I am using PDO with PHP to create a new database and then a
Hello i was trying to make function with PDO but getting error (new to
Here the thing, other PDO works well, but this one doesn't. I have tried
this here below is my class database in php <?php class DB { private
while using the following constructor $this->pdo = new PDO ($this->source, $this->username, $this->password); if I
I'm using this code and I'm beyond frustration: try { $dbh = new PDO('mysql:dbname='
<?php require 'dbinfo.php'; try { $db = new PDO($dsn, $username, $password); $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION
New to javascript, but I'm sure this is easy. Unfortunately, most of the google
Using PDO with prepared statements (I learned from here: http://www.kitebird.com/articles/php-pdo.html ) I'm using a

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.