hi there,
I have taken script which fucntion as mysql connection. but there are somethings that i dont understand.
does anyone can help me?
Thanks in advance
the code is below

function query($sql) {
	$link = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('There was a problem connecting to the database.');
	
	$stmt = $link->prepare($sql) or die('error');
	$stmt->execute();
	$meta = $stmt->result_metadata();

	while ($field = $meta->fetch_field()) {
		$parameters[] = &$row[$field->name];
	}

	$results = array();
	call_user_func_array(array($stmt, 'bind_result'), $parameters);

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

	return $results;
	$results->close();
	$link->close();
}

here i dont understand

while ($field = $meta->fetch_field()) {
		$parameters[] = &$row[$field->name];
	}

	$results = array();
	call_user_func_array(array($stmt, 'bind_result'), $parameters);

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

	return $results;

especially i dont understand from where did

$results[]
&$row[$field->name];

and

$x

come and didnt understand what does

&

mean
if anyone can help me to understand it line by line i would be grateful to that man.

Recommended Answers

All 4 Replies

$results[] is basically saying "add an element to the array called $results" - it's like "pushing" an element onto the end of an array - well, it doesn't necessarily have to mean the end of the array if you are using associative arrays, but in short it is assigning a new element to the $results array.

For example, let's say you have an array called $fruit.

You could do:

$fruit = array('apple', 'orange');

or you could do:

$fruit[] = 'apple';
$fruit[] = 'orange';

In this example, the same result is accomplished, but doing the second method is often preferred since it means you can append the array instead of redeclaring the whole thing. You can also append an array that was already created like this:

$fruit = array('apple', 'orange');
$fruit[] = 'kiwi';

Then you have 3 elements in the array.

The & before the variable name means you are referring to the item BY REFERENCE - not directly manipulating its value but calling it by reference. This concept is a bit different than normal calls to the variable itself. I myself don't write code by referring to things by reference, but it's basically because it's a concept I've managed to do without by finding alternate approaches for in every scenario. So someone else may be able to fill you in more on why they like writing things by reference and why you typically do that.

When it is saying $results[] = $x;, it is just saying, create a new element of the $results array by taking the value of the recently created $x array; here is where the $x array value gets set: $x[$key] = $val; - this is just like in the example I went over of defining elements of an array, but in this case, the $x array is associative (because $key is the index of the element).

Elements of arrays are accessed by their indexes. Normal arrays (non-associative) are accessed numerically where the first element is index 0, and the second element is index 1 (in programming counting begins at 0).

So if you wanted to get the "orange" element from our $fruit array, you could write:

echo $fruit[1];

Since the $key part of the $x array is making the array associative, you could then refer to the element of the array by its textual key. Here's an example associative array:

$countries = array("US"=>"United States", "MX"=>"Mexico", "CA"=>"Canada");

Here we could get "Mexico" by doing this:

echo $countries;

The "MX" is the "key" of the element. You could also add another element into the array like this:

$countries = "Sweden";

I hope I've helped you understand a thing or two at least a little bit more than I have in confusing you. >_<

hi again can you help to understand this code. here i tried to understand this but nothink i got.

<?php

function square(&$n)
{
    $n = $n*$n;
}
$number = 4;
square($number);
print $number;
?>

pls can you explain it with the simplest method
thanks in advance

That one is a lot simpler than the earlier stuff. This is basically just creating a function called "square" which returns the square root of a number.

The $n = $n*n may look weird, but it is just saying "whatever the number is, multiply it by itself".

In other words, in the example here, it says that $number is equal to 4, but after it is passed to the "square" function, $number will equal 16 - since 4 multiplied by itself is 16.

i knew it. but i dont understand that why without &sign it returns 4. but only with the sign it returns true. may be & this sign is used to pass value to only variables.
Thanks for attention

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.