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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:59:03+00:00 2026-06-04T09:59:03+00:00

Hello i am trying to make function with while loop in php but cant

  • 0

Hello i am trying to make function with while loop in php but cant getting write here is my code

 function mail_detail($mail_detail){

    $data= mysql_query("select * from messages where messages.to = '$mail_detail' and to_viewed = 0 ORDER BY messages.id DESC");
    while ($result= mysql_fetch_array($data)){
    return $result;
    }

}

and out put is

$mail_detail= mail_detail($userid)
echo '<li class="read">

               <a href="#">
                 <span class="message">'. $mail_detail['title'].'</span>
                    <span class="time">
                       January 21, 2012
                   </span>
                                </a>
        </li>';

i am not getting all values just getting one value please help
thx

  • 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-04T09:59:08+00:00Added an answer on June 4, 2026 at 9:59 am

    The return statement is terminating your loop and exiting the function.

    To get all values, add them to an array in the loop, and then return the array. Like this:

    $results = array();
    
    while ($result = mysql_fetch_array($data)) {
        $results[] = $result;   
    }
    
    return $results;
    

    on the side that receives the array

    $msgArray = mail_detail($mail_detail);
    
    foreach($msgArray as $msg) {
        //use $msg
    }
    

    To add on, a function is only able to return once (except for some special circumstances that you should not worry about). Therefore, the first time your function comes across a return statement, it returns the value and exits.

    This functionality of return can often be used to your advantage. For example:

    function doSomething($code = NULL) 
    {
        if ($code === NULL) {
            return false;
        }
    
        //any code below this comment will only be reached if $code is not null
        // if it is null, the above returns statement will prevent control from reaching 
        // this point
    
        writeToDb($code);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello i am trying to return a struct from a function but i cant
Hello guys look to my code I'm trying to make a program which asks
I am trying to do make a C++ hello world that supports Unicode, but
hello im trying to make something like github treeslider im new to javascript.. but
Hello this might be really silly question but I am trying to make markers
Im trying to make a function to run it later. In php 5.3.2-1 it
Hello I am trying to make a little spider. While I was building it
I am trying to make a Demo in Appcelerator here is the code for
Hello i'm trying to make a tag script for my website so each time
Hello I'm trying to convert youtube links into embed code. this is what I

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.