0

i wanted to display the output of the query, but i cant retrieve my variable $result mainly because it is on a different file but i used the require to get it but i still get the error variable not defined,i wanted to know how can i retrieve the $result that's been on function.php and used it on the search.php to display.

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>

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";
}   
?>

db.php

<?php

$connection = new mysqli('localhost', 'root', '',
    'loginapp');

    if(!isset($connection)){
        die("Database connection failed!");
    }
4
  • You're establishing $result in a function searchData() so it will be out of scope to use elsewhere. Try having searchData() return your result instead then set it like so $result = searchData(); Commented Sep 28, 2017 at 2:14
  • @AaronW. I'm getting error like Fatal error: Uncaught Error: Call to undefined method mysqli_result::fetch_rows() in C:\xampp\htdocs\udemy\mysql\search.php:33 Stack trace: #0 {main} thrown in C:\xampp\htdocs\udemy\mysql\search.php on line 33 Commented Sep 28, 2017 at 2:48
  • First try to call the function searchData() then try to see your query Commented Sep 28, 2017 at 3:02
  • already fix thank you guys :) Commented Sep 28, 2017 at 3:19

1 Answer 1

1

To expand on Arron W.'s comment, In your function.php file, you will need to return $result, then in search.php you will use load the return into a variable, then use that variable to access the data.

function.php

$result = mysqli_query($connection, $query);

echo "$result->num_rows". "found";
return $result
}   
?>

search.php

    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()){
            }
        }   
    ?>
Sign up to request clarification or add additional context in comments.

4 Comments

According to the comment-formatting page, by surrounding the code in backticks (`)
You probably have a $query left in your search.php.... replace them all with the $result
yeah my bad, and now this is the error that i'm getting Fatal error: Uncaught Error: Call to undefined method mysqli_result::fetch_rows() in C:\xampp\htdocs\udemy\mysql\search.php:33 Stack trace: #0 {main} thrown in C:\xampp\htdocs\udemy\mysql\search.php on line 33
Ahh yes... use fetch_row() instead of fetch_rows() FetchRow

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.