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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:45:28+00:00 2026-06-14T08:45:28+00:00

I have the following code which creates an array. $qry=SELECT foo_id, foo1, foo2 FROM

  • 0

I have the following code which creates an array.

$qry="SELECT foo_id, foo1, foo2 FROM foo_table";
$result=mysql_query($qry);

while($row = mysql_fetch_array($result)){
extract($row);  
$my_array[$foo_id] = array('foo1' => $foo1, 'foo2'  => $foo2);
}

If I save this in a separate file (array.php) and then ‘include’ this in my code (example 1) everything works fine and $my_array can be called fine. If however I set this code up as a function (example 2) and call it from within my code and then try and use the array I get the message that $my_array has not been defined.

example 1 – displays the array

include 'array.php';
var_dump($my_array[1]);

example 2 – gets the error message Undefined variable: $my_array

function create_array(){
  $qry="SELECT foo_id, foo1, foo2 FROM foo_table";
  $result=mysql_query($qry);

  while($row = mysql_fetch_array($result)){
  extract($row);    
  $my_array[$foo_id] = array('foo1' => $foo1, 'foo2'  => $foo2);
  }
}

create_array();
var_dump($my_array[1]);

Am I doing something fundamentally wrong? I’m a bit new to php so simple explanations would be appreciated!!

Thanks

  • 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-14T08:45:29+00:00Added an answer on June 14, 2026 at 8:45 am

    $my_array being defined inside a function, doesn’t exists outside that function.

    Try add return $my_array; in create_arry() function.

    Then call the function like this: $my_array = create_arry();, to store data returned by that funnction in $my_array variable.

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

Sidebar

Related Questions

I have the following code which uses an array to write the result to
I have the following code which creates a simple window with two buttons which
I have the following code which creates an ImageButton and plays a sound when
I have the following code which creates a new table. var html = '<table>';
I have the following code which definitely returns a proper data result if I
So i have the following code which is an array of python dictionaries userInfo.append({userType:blah,
I have the following code which has been passed on to me and creates
I have the following code which connects to a oracle database via soap, creates
I have some code that creates a multi-dimensional array, but my result seems to
I have code using QuickForm that creates a select widget with the following: $form->addElement(

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.