简体   繁体   中英

Can't echo single value from array with a function in PHP

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

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;

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!

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.

您需要将$query_result作为函数参数传递,或者将其定义为函数中的全局变量。

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