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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:56:42+00:00 2026-06-04T06:56:42+00:00

I have this old-style non-PDO MySQL query (the code isn’t tightened up, just to

  • 0

I have this “old-style” non-PDO MySQL query (the code isn’t tightened up, just to show exactly what I mean):

<?php
include('db_conn.php'); //Contains the connection info.

$category = 'cars';
$q = "SELECT * FROM `photos` WHERE `category` = '$category'";
$qResult = mysql_query($q);
$Numq = mysql_numrows($qResult);

$count = 0;
while ($count < $Numq)
{
$ThisID = mysql_result($qResult,$count,"id");
$ThisCaption = mysql_result($qResult,$count,"caption");
echo '<img src="images/'.$ThisID.'.jpg" alt="" />';
echo '<br />'.$ThisCaption.'<br /><br />';
$count++;
}
?>

I’d like to re-cast the query in PDO form (which I’m just learning). I’ve written this:

<?php
//I've set out the connection info format in case there's anything wrong with it...
$db = new PDO('mysql:host=my_host;dbname=my_db_name;charset=UTF-8', 'db_user', 'user_password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); //I prefer to throw PHP errors.
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$category = 'cars';
$statement = $db->prepare("SELECT * FROM `photos` WHERE `category`=?");
$statement->execute(array($category));
$statement->setFetchMode(PDO::FETCH_BOTH);

while ($result = $statement->fetch()) {
//$result[0] = the ID, $result[1] = the caption...
echo '<img src="images/'.$result[0].'.jpg" alt="" />';
echo '<br />'.$result[1].'<br /><br />';
}
?>

… In the “old” non-PDO form I can capture the ID and the caption just by specifying the column name.
… but in the PDO form I have to specify $result[0] and $result[1].
… How can I change the PDO form so that I don’t have to explicitly specify (“remember”) which member of the array is the ID, and which is the caption, etc (as the “old” method allows)?

  • 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-04T06:56:43+00:00Added an answer on June 4, 2026 at 6:56 am

    Instead of using PDO::FETCH_BOTH as fetching mode, you’ll probably want to use PDO::FETCH_ASSOC — to fetch your data as an associative array.

    Then you can access the members of the array by: $result['id'], $result['caption'], etc.

    PDO supports several interesting fetching modes ; including

    • associative-array : the keys of the array will be the column names, as returned from the database ; which is probably what you are used to
    • objects ; including instances of the class you specify

    To see what’s possible, you might want to take a look at the different PDO::FETCH_* constants — the list can be found here : Predefined Constants.

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

Sidebar

Related Questions

We have some code kicking around that uses this old internal Sun package for
I have this old jquery script for progressive disclosure: (notice the $(this).text('more...') code changes
I have this old Access database (2000 format) which i want to convert to
I'm reviewing my old algorithms notes and have come across this proof. It was
-- This is an updated post, old post removed -- Suppose I have data
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
In my current company, we have this decade old...let's call it a Hello World
Dear stackoveflow, I have this problem. I'm working with an old version of mssql
Historically I have always written my Exception handling code like this: Cursor cursor =
I am currently migrating my code from the old-style enum to enum class. The

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.