1

I have this PHP function :

function userParent($Username)
{
global $con;
$Result = mysqli_query($con, "SELECT Username FROM Family WHERE Parent = '$Username' LIMIT 10");
$row = mysqli_fetch_assoc($Result);
return $row; 
}

that function should give me 10 rows in array, but why I just have a value in array? I tried to test that codes outside function bracket and try to add WHILE loop like this :

while ($row = mysqli_fetch_assoc($Result)){
    print_r($row);
}

and it works. I got my 10 rows in array format. but it prints the result to the screen. how to make it as variable so it can be returned in function?

thanks.

UPDATE : according to Phil's answer, now here's my complete code :

<?php
function userParent(mysqli $con, $username) {
    $stmt = $con->prepare('SELECT Username FROM Family WHERE Parent = ? LIMIT 10');
    $stmt->bind_param('s', $username);
    $stmt->execute();
    $res = $stmt->get_result();
    return $res->fetch_all(MYSQLI_ASSOC);
}

$DbServer = 'localhost';
$DbUser = 'username';
$DbPassword = 'password';
$DbName = 'dbname';
$mysqli = new mysqli($DbServer, $DbUser, $DbPassword, $DbName);

$arrayParent = userParent($mysqli, 'root');
print_r($arrayParent);

?>

but I got this error message :

Fatal error: Call to undefined method mysqli_stmt::get_result() in /home/myhome/public_html/test.php on line 6

8
  • could you please tell me why what's wrong with global? Commented Apr 19, 2013 at 4:44
  • 1
    It`s a "Bad practice". Commented Apr 19, 2013 at 4:45
  • @CORRUPT : then should I put this row : $con = mysqli_connect($DbServer, $DbUser, $DbPassword, $DbName); on each function I have? Commented Apr 19, 2013 at 4:47
  • 1
    @RobertHanson c2.com/cgi/wiki?GlobalVariablesAreBad Commented Apr 19, 2013 at 4:47
  • 1
    Why are the queries different in your two examples? Commented Apr 19, 2013 at 5:36

2 Answers 2

3

Use return:

function userParent(mysqli &$dbms, $username){
    // You need to "escape" strings, which you would use in direct queries.
    // OR BETTER: use mysqli prepared statements with parameter binding.
    $username = mysqli_real_escape_string($dbms, $username);

    $result = mysqli_query($dbms, "SELECT Username FROM Family WHERE Parent = '$username' LIMIT 10");

    // Create temporary array for resultset:
    $buffer = array();

    // Fetch data to temporary buffer:
    while ($row = mysqli_fetch_assoc($result)){
        $buffer[] = $row;
    }

    // Free result set:
    $result->free();

    // Return buffer to global scope:
    return $buffer;
}

$users = userParent($con, 'John');

var_dump($users);
Sign up to request clarification or add additional context in comments.

3 Comments

You don't need to pass the mysqli instance by reference. Objects are implicitly passed by reference
@Phil, it might be, but I'm don't trust it. Just prefer a C++ way. Still, I know, that PHP-references are not C++ pointers. Thank you for suggestion.
Might want to watch out for parameters with apostrophes in them, eg O'Neil
2

Try mysqli_result::fetch_all instead

function userParent(mysqli $con, $username) {
    $stmt = $con->prepare('SELECT Username FROM Family WHERE Parent = ? LIMIT 10');
    if ($stmt === false) {
        throw new Exception($con->error, $con->errno);
    }
    $stmt->bind_param('s', $username);
    $stmt->execute();
    $res = $stmt->get_result();
    return $res->fetch_all(MYSQLI_ASSOC);
}

Then call it like this

$parents = userParent($mysqli, 'some username');

Read these in case you're not aware of prepared statements and parameter binding

Update

Apparently (undocumented), the mysqli_stmt::get_result() method is only available when using the mysqlnd driver. If you cannot use this driver, try this alternative

function userParent(mysqli $con, $username) {
    $stmt = $con->prepare('SELECT Username FROM Family WHERE Parent = ? LIMIT 10');
    if ($stmt === false) {
        throw new Exception($con->error, $con->errno);
    }

    $stmt->bind_param('s', $username);
    $stmt->execute();

    $parent = null;
    $parents = array();
    $stmt->bind_result($parent);
    while($stmt->fetch()) {
        $parents[] = $parent;
    }
    return $parents;
}

14 Comments

Provide him a procedural way, as he is using mysqli_()
@Mr.Alien Why? It makes no difference.
You don't use type hints when calling a function, they are only for the function signature. I've provided a usage example
@RobertHanson I've updated my answer. This is why I always recommend PDO over MySQLi
@RobertHanson Sounds like the prepare() failed. I've updated my answer with some error checking
|

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.