简体   繁体   English

无法使用PHP中的函数回显数组中的单个值

[英]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. 我在使用PHP时遇到问题,我使用MySQL查询的结果填充数组。

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 服务器正在运行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. 函数不会引入您未在函数内定义或传入的变量。由于您没有定义或传入$ query_result和$ result_array,因此脚本不起作用。 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. 要使函数工作,您需要做的就是传入变量$ query_result,$ result_array或在函数内定义它。

Edit: As Adam said, you can define in the function to use it as a global variable: global $query_result, $result_array; 编辑:正如亚当所说,你可以在函数中定义将它用作全局变量:global $ query_result,$ result_array;

Check your variable scoping. 检查变量范围。 Your function has no variable $query_result . 你的函数没有变量$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. 您需要将$query_result作为参数传递给函数。 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. 但是没有真正需要$i变量,因为您可以使用[]将新元素推送到数组中。

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. 您也可以使用global $query_result ,但显式参数通常是更好的编程风格。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM