2

I am having trouble with PHP, where I populate an array with results from a MySQL query.

The problem is when I make a function to echo a certain element of the array, it's not working, where as without a function there are no errors.

Establish connection, perform query, store result in variable:

require_once("db.php");

$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$query = "SELECT * FROM arlista";
$query_result = mysqli_query($conn, $query);

mysqli_close($conn);
$result_array = array();

I pass the query results to an array, then I want to query a single value from the array. The problem is if I use a function like this, this does not work. I can't get the element of the array to display in the browser.

function arlista($attr, $rownum){
    while($row = mysqli_fetch_array($query_result)){
        $result_array[$i] = $row[$attr];
        $i++;
    }
    echo $result_array[$rownum];
}

arlista("ar",1);

However this works if I do not use a function. The browser is displaying the value.

while($row = mysqli_fetch_array($query_result)){
$result_array[$i] = $row["ar"];
$i++;
}

echo $result_array[1];

Could someone explain what is going wrong with the function or how do I fix it to work? Thank you!

The server is running PHP 5.6.19

3
  • I do not see $i declared as 0 in function. Commented Apr 1, 2016 at 23:18
  • @cpugourou Undefined variables become 0 when used in an arithmetic context. Commented Apr 1, 2016 at 23:23
  • Thanks. Never thought of this one. Nevertheless I will continue to properly declare all my variables... kinda old school. Probably because I never use while to go across an array... Commented Apr 1, 2016 at 23:27

4 Answers 4

2

Check your variable scoping. Your function has no variable $query_result. Turning on error reporting would also give you notices about the problem.

Using a global would work:

function arlista($attr, $rownum){
    global $query_result, $result_array;
    while($row = mysqli_fetch_array($query_result)){
        $result_array[$i] = $row[$attr];
        $i++;
    }
    echo $result_array[$rownum];
}

arlista("ar",1);

Hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

2

A function does not bring in the variables that you haven't defined inside the function, or passed in. Since you did not define or pass in $query_result and $result_array, the script would not work. However, without the function, that variable is defined, so the script will work. To make the function work, all you need to do is pass in the variable $query_result, $result_array or define it inside the function.

Edit: As Adam said, you can define in the function to use it as a global variable: global $query_result, $result_array;

Comments

2

You need to pass $query_result as an argument to the function. You should also initialize the variables that you use within the function. But there's no real need for the $i variable, since you can use [] to push a new element onto an array.

function arlista($attr, $rownum, $query_result){
    $result_array = array();
    while($row = mysqli_fetch_array($query_result)){
        $result_array[] = $row[$attr];
    }
    echo $result_array[$rownum];
}

arlista("ar", 1, $query_result);

You could also use global $query_result, but explicit arguments are generally better programming style.

1 Comment

thank you very much for the explanation, I understand what was missing now. it's working nicely.
1

You need to either pass $query_result as a function parameter, or define it as a global variable within the function.

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.