Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 未定义变量,未定义索引-未使用全局$var解决_Php_Mysqli - Fatal编程技术网

Php 未定义变量,未定义索引-未使用全局$var解决

Php 未定义变量,未定义索引-未使用全局$var解决,php,mysqli,Php,Mysqli,我正在使用一个PHP函数,它使用几个不同的变量显示来自数据库的结果 在PHP脚本中,我在声明函数时声明了变量,如下所示: function printResults ($row, $result) 当我使用 require('includes/script.php'); printResults($row, $result); 我仍然得到一个错误,说 PHP注意:未定义变量:行 我也尝试过其他建议,比如使用global$row,但这并不能解决这个问题,这是我第一次使用函数,所以它把我甩了

我正在使用一个PHP函数,它使用几个不同的变量显示来自数据库的结果

在PHP脚本中,我在声明函数时声明了变量,如下所示:

function printResults ($row, $result)
当我使用

require('includes/script.php'); 

printResults($row, $result);
我仍然得到一个错误,说

PHP注意:未定义变量:行

我也尝试过其他建议,比如使用global$row,但这并不能解决这个问题,这是我第一次使用函数,所以它把我甩了

以下是具有中的函数的脚本:

$query = "SELECT title,description FROM mytable WHERE title LIKE '%$search%'";
$result = mysqli_query($mysqli, $query) or trigger_error("Query Failed! SQL: $query - Error: ". mysqli_error($mysqli), E_USER_ERROR);

function printResults ($row, $result) {
    if($result) {
        while($row = mysqli_fetch_assoc($result)) {
            echo '<div>'.$row['title'].'</div>';;   
        }
    }
}   

您不需要传递这个$row变量,它是在函数中定义的

$query = "SELECT title,description FROM mytable WHERE title LIKE '%$search%'";

$result = mysqli_query($mysqli, $query) or trigger_error("Query Failed! SQL: $query - Error: ". mysqli_error($mysqli), E_USER_ERROR);

function printResults ($result) {
    if($result) {
        while($row = mysqli_fetch_assoc($result)) {
            echo '<div>'.$row['title'].'</div>';;   
        }
    }
}   
printResults($result);

我看不到任何$row的声明。您只需要将结果传递给函数。我认为您不能在定义$row之前打印$row。当您在函数调用中使用$row变量时,没有定义$row变量。更重要的是。错误声明是在script.php中还是在主php中?我已经从函数声明以及运行函数的页面中删除了变量,但是出现的查询的标题不再存在,我该如何修复?