1

I'm new in php function is it possible to use mysqli_fetch_assoc in php function?

here's what I'm doing.

function.php

<?php
        function getinfoid($conn,$idapplicant){

        $sql = "SELECT * FROM applicants WHERE id_mis = '".$idapplicant."' ";
        $result = mysqli_query($conn,$sql);
        while ($row = mysqli_fetch_assoc($result)) {
        $prefix=$row['prefix'];
        $fname1=$row['fname'];
        $mname1=$row['mname'];
        $lname1=$row['lname'];
        $gender=$row['gender'];
        $addr=$row['addr'];
        }
    }
?>

view.php

    <?php 
    require('connection.php'); 
    include ('function.php');
    getinfoid($conn,'2');
    ?>
I'm <?php echo $fname1 ?>

how is it possible to echo $fname1 ?

2
  • 1
    Return a value from the function Commented Apr 13, 2016 at 9:36
  • You can build an array and return the array from which you can fetch the corresponding value. Commented Apr 13, 2016 at 9:37

4 Answers 4

1

Your function doesn't return anything, those assigned variables stay in the scope of the function. If there is always just one row to fetch, you can do it like this:

function getinfoid($conn,$idapplicant){
    $sql = "SELECT * FROM applicants WHERE id_mis = '".$idapplicant."' ";
    $result = mysqli_query($conn,$sql);
    return mysqli_fetch_assoc($result);
}

Then you can call this function and echo your variable:

$row = getinfoid($conn,'2');
?>
I'm <?php echo $row['fname'] ?>
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming you need to print only $fname1, following is my proposed solution:

You need to return $fname1 from your function.

<?php
function getinfoid($conn,$idapplicant){
    $sql = "SELECT * FROM applicants WHERE id_mis = '".$idapplicant."' ";
    $result = mysqli_query($conn,$sql);
    $row = mysqli_fetch_assoc($result);
    return $row['fname'];
} 
require('connection.php'); 
include ('function.php');
$fname1 = getinfoid($conn,'2');
?>
I'm <?php echo $fname1 ?>

Alternatively, you can return whole row from your function like this:

function getinfoid($conn,$idapplicant){
    $sql = "SELECT * FROM applicants WHERE id_mis = '".$idapplicant."' ";
    $result = mysqli_query($conn,$sql);
    return mysqli_fetch_assoc($result);
}
require('connection.php'); 
include ('function.php');
$useDetails = getinfoid($conn,'2');
?>
I'm <?php echo $useDetails['fname'] ?>

1 Comment

Probably remove the loop and return all the selected columns in an array and let the caller fetch any specific field?
0

You can declare variable $fname1 as global and access the variable. Assuming only single record is returned.

function.php

<?php
        $fname1 = "";
        function getinfoid($conn,$idapplicant){
        global $fname1;
        $sql = "SELECT * FROM applicants WHERE id_mis = '".$idapplicant."' ";
        $result = mysqli_query($conn,$sql);
        while ($row = mysqli_fetch_assoc($result)) {
        $prefix=$row['prefix'];
        $fname1=$row['fname'];
        $mname1=$row['mname'];
        $lname1=$row['lname'];
        $gender=$row['gender'];
        $addr=$row['addr'];
        }
    }
?>

view.php

<?php 
require('connection.php'); 
include ('function.php');
getinfoid($conn,'2');
?>
I'm <?php echo $fname1 ?>

Comments

0
<?php
        function getinfoid($conn,$idapplicant){

        $sql = "SELECT * FROM applicants WHERE id_mis = '".$idapplicant."' ";
        $result = mysqli_query($conn,$sql);
        $row = mysqli_fetch_assoc($result); 
        return $row
    }
?>

 <?php 
    require('connection.php'); 
    include ('function.php');
    $data = getinfoid($conn,'2');
    ?>
I'm <?php echo $data['firstname'] ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.