0

I'm writing a small class for a website, below is my code:

    class Main {
        public function GetLang() {
            global $link ;
            $get_lang_id = $link->query("SELECT `lang_id`,`lang_short`, `lang_name`, `lang_dir` FROM langs");
            return $get_lang_id;
        }
    }

Now to the question. instead of doing

$Main = new Main;
$GetLangOptions = $Main->GetLang();
while($LangOptions = $GetLangOptions -> fetch_array()){
//do something
}

can i add the fetch_array() chunk to the function GetLang() like below?

class Main {
    public function GetLang() {
        global $link ;
        $get_lang_id = $link->query("SELECT `lang_id`,`lang_short`, `lang_name`, `lang_dir` FROM tjcg_langs");
        $LangDetails = $get_lang_id -> fetch_array();
        return $LangDetails;
    }
 }

and if yes .. how can i get the results after calling the function?

SOLUTION: Using Santosh Achari's answer with the help of the voted answer here i managed the following:

class Main {
    public function GetLang() {
        global $link ;
        $get_lang_id = $link->query("SELECT `lang_id`,`lang_short`, `lang_name`, `lang_dir` FROM tjcg_langs");
        $rows = array();
        while($get_lang=$get_lang_id->fetch_array(MYSQLI_ASSOC)){
        $rows[] = $get_lang;}
        return $rows;
    }
}
    $Main = new Main;
    $GetLangOptions = $Main->GetLang();
    foreach($GetLangOptions  as $LangOption){
    echo $LangOption['lang_short'];
}
0

2 Answers 2

0

Do you mean using the $LangDetails variable? You can use it in a foreach statement or while loop to print the results.

$Main = new Main;
$GetLangOptions = $Main->GetLang();
foreach($GetLangOptions  as $LangOption){
echo $LangOption["column1"];
}

Foreach is easy and manageable. But you can also use while loop

$Main = new Main;
$GetLangOptions = $Main->GetLang();

while (list ($key, $val) = each ($GetLangOptions ) ) {
echo $val['coloumn1'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

using your suggestion mixed with the answer voted on stackoverflow.com/questions/3442895/… i did it .. thanks!
0

You seem to have the answer in the title itself. Use fetch_array() instead of fetch_assoc()

 class Main {
        public function GetLang() {
            global $link ;
            $get_lang_id = $link->query("SELECT `lang_id`,`lang_short`, `lang_name`, `lang_dir` FROM langs");
            return $get_lang_id;
        }
        public function GetUserDetails($user_id) {
            global $link ;
            $get_user_details = $link->query("SELECT `tjcg_users`.`user_id`, `tjcg_users`.`user_full_name`, `tjcg_users`.`user_name`, `tjcg_users`.`user_pass`, `tjcg_users`.`user_email`, `tjcg_users`.`user_date`, `tjcg_users`.`user_level`, `tjcg_users`.`user_session_token`, `tjcg_users`.`user_photo`, `tjcg_users`.`user_gender`, `tjcg_users`.`user_last_login` FROM tjcg_users where user_id= '$user_id'");
        $UserDetails = $get_user_details -> fetch_array(MYSQLI_ASSOC);
            return $UserDetails ;
        }
    }

In case you want to use fetch_assoc, you can also build the array inside your function GetUserDetails() and return it.

5 Comments

how do i get the results?
how do i get the results? I mean in my code above i would use a while loop something like: `$Main = new Main; $GetLangOptions = $Main->GetLang(); WHILE($LangOptions = $GetLangOptions -> fetch_array()){//do something}
$GetUserDetails = $Main->GetUserDetails($_SESSION['myID']); $GetUserDetails would contain your results.
sorry about the mess i edited the main post according to your suggestion. can you see it (more tidy)
Please read the first example in php docs us2.php.net/manual/en/mysqli-result.fetch-array.php. It will help you achieve what you want. You are not using fetch_array() correctly in your above post.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.