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

  • Home
  • SEARCH
  • 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 8543505
In Process

The Archive Base Latest Questions

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

I am trying to fetch results from a database, it returns no errors and

  • 0

I am trying to fetch results from a database, it returns no errors and it returns no results. Any ideas as to what I am doing wrong?

try {
    $db_connection = new PDO("mysql:host = $hostname; dbname = $database", $username, $password);
    //$db_connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT );  
    $query = $db_connection->prepare("SELECT `id`, `code` FROM `mycodedatabase` WHERE 1");
    $query->execute();
    while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
        echo $row['id'];
        echo $row['code'];
    }
    $db_connection = null;
}
catch(PDOException $e) {
    echo $e->getMessage();
}
  • 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-11T12:19:06+00:00Added an answer on June 11, 2026 at 12:19 pm

    Try to avoid using the same variable for different purposes. Use $query and $result. Don’t make your code complex. I think it will help while debugging and avoid errors when logic seems correct.

    <?php
    
    $pdo = new PDO('mysql:host=localhost;dbname=$database', "user", "pass");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $query = "SELECT id,code FROM mycodebase";
    $result = $pdo->prepare($query);
    $result->execute();
    
    while ($row = $result->fetch()) {
        echo $row['id'];
        echo $row['code'];
    }
    

    Don’t forget to change "user" and "pass"

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

Sidebar

Related Questions

I'm working with JSONP trying to fetch some results from the WOT API. See
I am trying to fetch records from Oracle 9i database whose value are 'FOO
I am retrieving a fetch from my core data database and trying to iterate
Im trying to develop a small site which retrieves a results from a database
I am trying to return results from my database so that I can create
I am trying to get results from a MySQL database by searching multiple terms
When trying to return a simple set of results from my database table 'checklist'
I am trying to fetch data from SQLite database(NSArray of strings) and populate the
I'm trying to fetch data from a page that resides on an intranet. The
I am trying to fetch a gzip from a URL and import it automatically

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.