从另一个php文件检索变量
我想显示查询的输出,但我无法检索我的变量$result,主要是因为它位于不同的文件中,但我使用了require来获取它,但我仍然得到未定义的错误变量,我想知道如何检索function.php上的$result,并在search.php上使用它来显示 search.php从另一个php文件检索变量,php,mysql,Php,Mysql,我想显示查询的输出,但我无法检索我的变量$result,主要是因为它位于不同的文件中,但我使用了require来获取它,但我仍然得到未定义的错误变量,我想知道如何检索function.php上的$result,并在search.php上使用它来显示 search.php <?php include "db.php"; include "function.php"; if(isset($_GET['keywords'])){ global $connection; $keyword =
<?php
include "db.php";
include "function.php";
if(isset($_GET['keywords'])){
global $connection;
$keyword = ($_GET['keywords']);
searchData($keyword);
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="search.php" method="GET">
<label>
Search
<input type="text" name="keywords">
</label>
<input type="submit" name="search">
<div class="form-group">
<div class="result-count">
Found <?php echo $query->num_rows; ?>result
</div>
<?php
if($query->num_rows){
while($r = $query->fetch_rows()){
}
}
?>
</div>
</form>
</body>
</html>
if(isset($_GET['keywords'])){
global $connection;
$keyword = ($_GET['keywords']);
$result = searchData($keyword);
}
...
<div class="result-count">
Found <?php echo $result->num_rows; ?>result
</div>
<?php
if($result->num_rows){
while($r = $result->fetch_row()){
}
}
?>
搜寻
发现结果
function.php
<?php require_once "db.php";?>
<?php
function searchData($keyword){
global $connection;
$query = ("
SELECT username
FROM users
WHERE username LIKE '%{$keyword}%'
");
$result = mysqli_query($connection, $query);
echo "$result->num_rows". "found";
}
?>
$result = mysqli_query($connection, $query);
echo "$result->num_rows". "found";
return $result
}
?>
要扩展Arron W.的注释,在function.php文件中,需要返回$result,然后在search.php中使用将返回值加载到变量中,然后使用该变量访问数据
function.php
<?php require_once "db.php";?>
<?php
function searchData($keyword){
global $connection;
$query = ("
SELECT username
FROM users
WHERE username LIKE '%{$keyword}%'
");
$result = mysqli_query($connection, $query);
echo "$result->num_rows". "found";
}
?>
$result = mysqli_query($connection, $query);
echo "$result->num_rows". "found";
return $result
}
?>
search.php
<?php
include "db.php";
include "function.php";
if(isset($_GET['keywords'])){
global $connection;
$keyword = ($_GET['keywords']);
searchData($keyword);
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="search.php" method="GET">
<label>
Search
<input type="text" name="keywords">
</label>
<input type="submit" name="search">
<div class="form-group">
<div class="result-count">
Found <?php echo $query->num_rows; ?>result
</div>
<?php
if($query->num_rows){
while($r = $query->fetch_rows()){
}
}
?>
</div>
</form>
</body>
</html>
if(isset($_GET['keywords'])){
global $connection;
$keyword = ($_GET['keywords']);
$result = searchData($keyword);
}
...
<div class="result-count">
Found <?php echo $result->num_rows; ?>result
</div>
<?php
if($result->num_rows){
while($r = $result->fetch_row()){
}
}
?>
if(isset($\u GET['keywords'])){
全球美元连接;
$keyword=($_GET['keywords']);
$result=searchData($keyword);
}
...
发现结果
您正在函数searchData()
中建立$result
,因此它将超出在其他地方使用的范围。尝试让searchData()
返回您的结果,然后像这样设置$result=searchData()代码>@AaronW。我得到了类似致命错误的错误:未捕获错误:调用C:\xampp\htdocs\udemy\mysql\search.php中未定义的方法mysqli_result::fetch_rows()。php:33堆栈跟踪:#0{main}抛出于C:\xampp\htdocs\udemy\mysql\search.php第33行首先尝试调用函数searchData()
然后尝试查看您的QueryReady修复程序(谢谢大家:)根据页面,通过在backticks(`)中包围代码,您的搜索中可能还有一个$query。php。。。。将它们全部替换为$resultyah my bad,现在这是我得到的错误致命错误:未捕获错误:调用C:\xampp\htdocs\udemy\mysql\search.php中未定义的方法mysqli_result::fetch_rows()。33堆栈跟踪:#0{main}抛出到C:\xampp\htdocs\udemy\mysql\search.php的第33ah行是。。。使用fetch_row()而不是fetch_rows()