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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:41:19+00:00 2026-06-11T21:41:19+00:00

questions from a Newbie :-) I am trying to convert my MySQL query syntax

  • 0

questions from a Newbie 🙂

I am trying to convert my MySQL query syntax into PDO but having some issues getting started.

I have an included file in my page called dbc.php. contains the code:

define ("DB_HOST", "localhost"); // set database host
define ("DB_USER", "dbuser"); // set database user
define ("DB_PASS","dbpass"); // set database password
define ("DB_NAME","dbname"); // set database name

$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");
$db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database");

My Page code is then:

<?php
include 'dbc.php';
page_protect();

function get_users($db) {   
$getusers = $db->query('SELECT employeeid, fullname FROM Persons order by fullname asc');
}

?>

I want to display the table output with the following code:

<table>
    <tr>
        <th> Full Name</th>
    </tr>
    <?                      
        while($row = $getusers->fetch(PDO::FETCH_ASSOC)) 
        {
    ?>
    <tr>
        <td>
        <?
        echo $row['fullname'];
        ?>
        </td>
    </tr>
        <?
        }
        ?>
</table>

This currently outputs the error:

Fatal error: Call to a member function fetch() on a non-object in /home/she/public_html/versionfour/people.php on line 170

where line 170 is my ‘ while($row = $getusers->fetch(PDO::FETCH_ASSOC)) ‘ statement.

I know I am missing something simple here, probably for not completely understanding PDO as yet.

any help is apprecaited.

Thanks and regards,
R

  • 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-11T21:41:20+00:00Added an answer on June 11, 2026 at 9:41 pm

    This issue you have relates to the scope of the “$getUsers” variable. Because the function does not return its value the HTML file cannot access this and the fetch() method is trying to be called on a undefined value, hence the “non-object” part within the error.

    <?php
      /** Include the db file which should define a connection as so
      $db = new PDO('dblib:host=$hostname;dbname=$dbName', $user, $pass); etc
      **/
      include 'dbc.php';
    
      page_protect();
    
      function get_users($db) 
      {   
        $result = $db->query("SELECT employeeid, fullname FROM Persons order by fullname asc");
        return $result;
      }
      $users = get_users($db);
    
      /** $users->fetch() can now be used **/
    
    ?>
    

    You should also consider some error checking to ensure that the returned db result is actually a PDO result or you may still encounter this issue.

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

Sidebar

Related Questions

ES 2.0 newbie here. I'm currently trying to make some 3D interlaced images from
Please excuse my newbie Question. I'm tryin to display data from a mysql table
Some related questions I found from searching: How does the 'fx' queue in jquery
Java newbie. I am trying to run a java main class from cmd line
preemptive sorry for the newbie question. I'm trying to load strings from a resource
Apologies for these recent newbie questions, but I'm obviously not asking the search engines
Disclaimer: The author is a newbie in OTP having some basic knowledge of Erlang's
Good day, friends. Once again stupid question about Obj-C from newbie :) I'm trying
I am not a total newbie but I am trying to install modules for
this is a two part question from a JS newbie. So, I was trying

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.