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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:47:06+00:00 2026-05-26T15:47:06+00:00

might be a basic one but i wrote a function that selects all from

  • 0

might be a basic one but i wrote a function that selects all from a dbtable in backend. But in front end, i want to use it with foreach to display results wherever i want.

Array(
[0] => Array
    (
        [stockCatID] => 1
        [stockCatName] => Copper
        [stockParentCat] => 0
    )

[1] => Array
    (
        [stockCatID] => 2
        [stockCatName] => Zinc
        [stockParentCat] => 0
    )
)

When I send resultset to my front-end page, this array shows up. So I can’t basically play it with:

<?php
$r = getAll("stockcategories");
foreach($r as $k=>$v) {
    echo ("<p><strong>$k</strong>: $v</p>");   
} 

With above result array iu outputs:

0 = Array1 = Array

Added: I don’t want to echo from backend function.

So finally it is my function:

<?php
function getAll ($tableName,$orderBy="", $limit="") {
        $orderBy = $orderBy == "" ? $orderBy : (" ORDER BY =\"".$orderBy."\" "); 
        $limit = $limit == "" ? $limit : (" LIMIT =\"".$limit."\" ");
        $q  = mysql_query("SELECT * FROM $tableName $orderBy $limit");
        if (!$q) { die('Could not connect: ' . mysql_error());} else { $num=mysql_numrows($q); 
        if ($num != 0 ) { 
            while($r = mysql_fetch_assoc($q)) { 
                $rArray[] = $r;
            }
            mysql_free_result($q); 
            return $rArray; 
            } else { echo '<span class="notification n-error">No Record Found</span>'; return false; }        
    }
?>

Thanks for any help.

  • 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-05-26T15:47:06+00:00Added an answer on May 26, 2026 at 3:47 pm

    $rArray is a 2 dimensional array but you are accessing it in only one dimension. You need nested foreach loops. The outer loop iterates over each row returned, and the inner iterates over the columns in each row:

    foreach ($r as $row) {
      foreach($row as $k=>$v) {
        echo ("<p><strong>$k</strong>: " . htmlspecialchars($v) . "</p>");   
      }
    } 
    

    Update a function to iterate over both dimensions:

    function printResults($resultArray) {
        foreach ($resultArray as $row) {
          foreach($row as $k=>$v) {
    
            // Note sanitizing against XSS if this was user-input...
            echo ("<p><strong>$k</strong>: " . htmlspecialchars($v) . "</p>");   
          }
        } 
    
    }
    

    Call it as:

    $r = getAll("stockcategories");
    printResults($r);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like one of the most basic things that one might want to
This might be confusing but what I want to do is a basic select
Ok, this might be basic, but I want good programmers' opinions on this. What
How might one retrieve the last element of a string array in visual basic
This might be basic question but how do I create a list of lists
Know this might be rather basic, but I been trying to figure out how
This might look like a basic question to some of you but I expect
What I'm trying to do is rather basic, but I might have my facts
I know, this might be a very basic question but I am not 100%
I have another SQL/access 2007 question that seems really basic but I'm not sure

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.