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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:40:10+00:00 2026-06-03T10:40:10+00:00

Yesterday another user helped out with building a generic function for handling MySQL Queries.

  • 0

Yesterday another user helped out with building a generic function for handling MySQL Queries. It looks like this:

function fetchAll($query) {
  $res = mysql_query($query) or trigger_error("db: ".mysql_error()." in ".$query);
  $a   = array();
  if ($res) {
    while($row = mysql_fetch_assoc($res)) { 
        $a[]=$row;
    }
  }
  return $a;
}

And to output the returned results I simply do the following:

$data = fetchAll("SELECT * FROM news_table ORDER BY id LIMIT 10");

foreach ($data as $row) {
    echo $row['title'];
}

My question relates to outputting the result when there’s only one result in the array. Like when displaying the current blog post of a page. I want to know if I can do it without first calling the foreach loop? Is there a way to output just the first result from the array as I do not need to loop through it.

Perhaps I could have an alternate function, as opposed to the fetchAll() one above? One that just outputs one row?

Cheers,

Scott

  • 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-03T10:40:12+00:00Added an answer on June 3, 2026 at 10:40 am

    Yes. For example:

    echo $data[0]['title'];
    

    Basically your $data is a two-dimensional array, with the first dimension being the row number (count starts with 0), so you can access any of the rows directly if you know its number. If you only have one row, it’s necessarily 0.

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

Sidebar

Related Questions

Yesterday, I began encountering '999' errors on Yahoo-Pipes. It looks like they throttle requests
Yesterday I had problems with another boost functions but luckily you guys helped me
This question is related to another question which I asked yesterday! List all links
In another question , a user pointed out that the new keyword was dangerous
Yesterday, I found myself writing code like this: SomeStruct getSomeStruct() { SomeStruct input; cin
I asked this question yesterday got a pretty good answer but can't figure out
I already asked a question about this yesterday, but now I have another :)
I have this regex which some kind folks on SO helped me with yesterday.
We got pitched this idea yesterday. A user visits our site and are marked.
Yesterday I asked another question to calculate time. Now I realized, that it makes

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.