-1

I have search box in home.php

<form action ="search.php" method = "post">

<input name="search" type="text" size="30" placeholder="...."/>

<input type="submit" value="Search"/>

<?php print ("$output");?>

And serach.php file

<?php
require_once("class.user.php");
require_once("class.db.php");
$output = '';

if(isset($_POST['search'])) {
$search = $_POST['search'];
$search = preg_replace("#[^0-9a-z]i#","", $search);

$connection = new DB_con();

$query = $mysqli->query("SELECT * FROM users WHERE username LIKE '%".$search."%' AND email LIKE '%".$search."%'") or die ("Could not search");
$result =$mysqli->query($connection, $query);
$count = $result->num_rows;

if($count == 0){
  $output = "There was no search results!";

}else{

  while ($row = $result->fetch_array($query)) {

    $username = $row ['username'];
    $email = $row ['email'];


    $output .=$username.''.$email;

  }

}
}
?>

I have several errors. Notice: Undefined variable: mysqli in search.php on line 12

Fatal error: Uncaught Error: Call to a member function query() on null in \search.php:12 Stack trace: #0 {main} thrown in search.php on line 12

B001ᛦ
  • 2,083
  • 6
  • 22
  • 31
ZuZu
  • 1
  • 1
  • this is what u need `$query = "SELECT * FROM users WHERE username LIKE '%".$search."%' AND email LIKE '%".$search."%'"); $result =$mysqli->query($connection, $query);` Consider also using prepared statements – Masivuye Cokile Sep 28 '17 at 13:18
  • `$connection = new DB_con();` ahem! – Funk Forty Niner Sep 28 '17 at 13:25
  • @MasivuyeCokile Still same.I've heard about prepared statements, but i'm still beginner and this is learning process :) Thanks for advice! – ZuZu Sep 28 '17 at 13:26
  • plus, you're querying twice, and with the wrong variable I might add. – Funk Forty Niner Sep 28 '17 at 13:26
  • it seems like you are defining `$connection` as your main connection to the database and then you use `$mysqli` without defining it. Consider doing something like `$db = new mysqli('hostname', 'username', 'password', 'dbname'); $results = $db->query('query');`. You are mixing object oriented programming with procedural programming. You can read the [manual](http://php.net/manual/en/mysqli.query.php) and find out more about msqli and different ways of connecting to the database. – Antoniu Livadariu Sep 28 '17 at 13:29
  • @Fred-ii- Thanks! i fixed it ;) Thanks again – ZuZu Sep 28 '17 at 15:09
  • @AntoniuLivadariu Thanks Antoniu, I fixed that problem... I was little bit confused :) – ZuZu Sep 28 '17 at 15:11

1 Answers1

2

$mysqli is undefined hence you getting the error, and also you are doing query twice.

<?php
require_once("class.user.php");
require_once("class.db.php");
$output = '';

if (isset($_POST['search'])) {
    $search = $_POST['search'];
    $search = preg_replace("#[^0-9a-z]i#", "", $search);

    $connection = new DB_con();

    $query = "SELECT * FROM users WHERE username LIKE '%" . $search . "%' AND email LIKE '%" . $search . "%'";
    if ($result = $connection->query($query)) {
        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc($query)) {

                $username = $row['username'];
                $email    = $row['email'];


                $output .= $username . '' . $email;

            }
        } else {

            $output = "There was no search results!";
        }


    } else {

        printf("Errormessage: %s\n", $connection->error);
    }

}
?>
Funk Forty Niner
  • 74,372
  • 15
  • 66
  • 132
Masivuye Cokile
  • 4,729
  • 3
  • 18
  • 34