简体   繁体   中英

Echo from function php

hello i want to echo a result from functions

code

function AboutUser()
{
    global $Connection;

    $GetUsers = mysqli_query($Connection, "SELECT * FROM users WHERE username='GentritAbazi'");
    while($Show_Users = mysqli_fetch_array($GetUsers))
    {
        return $SignupDate = $Show_Users['signup_date'];
        $Email = $Show_Users['email'];
        $Gender = $Show_Users['gender'];
        $Country = $Show_Users['country'];
    }
}

Now my code not work

AboutUser()

how to do this ?

Because you return $SignupDate = $Show_Users['signup_date'];

You want to echo, not return.

Let's use this in the while loop.

echo $Show_Users['signup_date'] ."<br>";
echo $Show_Users['email']  ."<br>";
echo $Show_Users['gender']  ."<br>";
echo $Show_Users['country'] ."<br>";
echo '<hr>'

But that is most elegant, if you collect all the data into a big array, and loop through that array.

return mysqli_fetch_all($GetUsers);

Based on the comments, and after I realized, you probably want to get one users data, here is the updated code:

function AboutUser($userName) {
    global $Connection;
    $res = mysqli_query($Connection, "SELECT * FROM users WHERE username='".  mysqli_real_escape_string($Connection, $userName)."'");
    return mysqli_fetch_row($res);
}

$userData = AboutUser('GentritAbazi');
if (!empty($userData)) {
    echo $userData['signup_date'] ."<br>";
    echo $userData['email']  ."<br>";
    echo $userData['gender']  ."<br>";
    echo $userData['country'] ."<br>";
}
function AboutUser()
{
    global $Connection;

    $GetUsers = mysqli_query($Connection, "SELECT * FROM users WHERE username='GentritAbazi'");
    while($Show_Users = mysqli_fetch_array($GetUsers))
    {
        echo $Show_Users['signup_date'];
        echo $Show_Users['email'];
        echo $Show_Users['gender'];
        echo $Show_Users['country'];
    }
}

You can have the function echo the value instead of returning it, like mentioned above.

Or you can use special tags eg

<?= 
AboutUser(); 
?>

Hopefully this works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM