/    Sign up×
Community /Pin to ProfileBookmark

help with understanding call_user_func_array and & reference operator

I have script that retrieves information from database.
but here i dont understand
the call_user_array_func() and
referense operator why used here?

[code=php]<?php

function read()
{
$parameters = array();
$results = array();

$mysql = new mysqli(‘localhost’, ‘root’, ‘root’, ‘db’) or die(‘There was a problem connecting to the database’);
$stmt = $mysql->prepare(‘SELECT body FROM posts’) or die(‘Problem preparing query’);
$stmt->execute();

$meta = $stmt->result_metadata();

while ( $field = $meta->fetch_field() ) {

$parameters[] = &$row[$field->name];
}

call_user_func_array(array($stmt, ‘bind_result’), $parameters);

while ( $stmt->fetch() ) {
$x = array();
foreach( $row as $key => $val ) {
$x[$key] = $val;
}
$results[] = $x;
}

return $results;

}

$results = read();
?>
<!DOCTYPE html>

<html lang=”en”>
<head>
<meta charset=”utf-8″>
<title>untitled</title>
</head>
<body>
<?php foreach ($results as $row) : ?>

<p> <?php echo $row[‘body’]; ?> </p>
<?php endforeach; ?>
</body>
</html>
[/code]

i know this & reference operator but for what reason this is used i cannot understand. this section

[code=php] while ( $field = $meta->fetch_field() ) {

$parameters[] = &$row[$field->name];
}[/code]

and from where $row variable has come?

[code=php] while ( $stmt->fetch() ) {
$x = array();
foreach( $row as $key => $val ) {
$x[$key] = $val;
}
$results[] = $x;
}
[/code]

if possible pls explain step by step what happens in this script
Thanks all for attention

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@coldfire421Apr 13.2011 — It use to call a user function given with an array of parameters. let say for example you want to pass a function name as a parameter to other function and call it inside that function, you can use the [COLOR=#000000][COLOR=#0000BB]call_user_func_array[COLOR=Black][/COLOR][/COLOR][/COLOR]
Copy linkTweet thisAlerts:
@azegurbauthorApr 13.2011 — thank you very much for attention. Can you explain the code i have posted?
Copy linkTweet thisAlerts:
@NogDogApr 13.2011 — I cannot figure out where $row comes from. Does this code work? I would have thought that this line...
[code=php]
while ( $field = $meta->fetch_field() ) {
[/code]
...should instead be...
[code=php]
while ( $row = $meta->fetch_field() ) {
// ^^^^
[/code]
Copy linkTweet thisAlerts:
@azegurbauthorApr 13.2011 — thank you for attention. original source is

http://net.tutsplus.com/tutorials/php/the-problem-with-phps-prepared-statements/

and i tested it works. test
×

Success!

Help @azegurb spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...