致命错误:在第43行的C:\ xampp \ htdocs \ projectname \ searchit.php中的非对象上调用成员函数mysqli_fetch_array()[关闭]

致命错误:在第43行的C:\ xampp \ htdocs \ projectname \ searchit.php中的非对象上调用成员函数mysqli_fetch_array()[关闭]

问题描述:

I know this question has been asked several times, however I can still not get this to work!!!

I am trying to make a basic search engine which I manually enter pages etc in a phpMyadmin mysql databse. The error that keeps coming up is:

Fatal error: Call to a member function mysqli_fetch_array() on a non-object in C:\xampp\htdocs\projectname\searchit.php on line 43

So here is the code!!!

<html lang="en">
<head>
<meta charset="utf-8">
<title>
Search The Internet
</title>
<link rel="stylesheet" href="main.css">
<div id="menu_bar1" style="padding:10px; margin: 0px;">
<form action="searchit.php" method="GET"> 
<label>Search
</label>
<input type="text" name="lets_search_for" size="50" placeholder="What do you want to search for???"/>
<input type="submit" name="search" value="Lets Search!!!"/>
</form>
</div>
</head>
<body bgcolor="#8E44AD">    
<?php    
  if (isset($_GET['search']))    
  {    
    $mysqli = mysqli_connect("localhost", "root", "Password");  
    $search_val = $_GET['lets_search_for'];
    echo "What Did We Find??? For <b><i> $search_val </i></b>" ;
    print "<br />";    
    $query = "SELECT * FROM search_websitename WHERE keywords LIKE ' . %$search_val% .'"; 
    $result = $mysqli->query($query);

   if($mysqli === FALSE) {
        die(mysqli_error()); 
   }    
   while ($row = $result->mysqli_fetch_array($query) ){  //This is line 43!!!

      $title = $row['title'];
      $link = $row['url'];
      $des = $row['des'];   

      echo "<a href='$link'>$title</a><br />";
      echo $link;
      echo "<p>" .
      $des . "</p><hr /><br />";    
    }    
}    
?>    
</body>    
</html>

You need to fix the following:

$mysqli = mysqli_connect("localhost", "root", "Password");
// missing database connection

$search_val = $_GET['lets_search_for'];

$query = "SELECT * FROM search_websitename WHERE keywords LIKE ' . %$search_val% .'";
// you do not need a dot (concatenation)

So finally:

$mysqli = mysqli_connect("localhost", "root", "Password", "DATABASE_NAME");
$search_val = $_GET['lets_search_for'];
$search_val = '%' . $mysqli->real_escape_string($search_val) . '%';
$query = "SELECT * FROM search_websitename WHERE keywords LIKE '$search_val' ";
$result = $mysqli->query($query);
while ($row = $result->fetch_assoc() ) {
    echo $row['whaatever_column_name'];
}

This error message means that you are trying to call a method on an object, which isn't actually an object. This usually means, as is the case for you, that it ought to be an object, but somehow, it never actually became so. In your case, it ought to have become an object here

$result = mysqli->query($query);

If all goes to plan, $result becomes an object, which has access to the mysqli_fetch_array() method. However, your error message means that $result never became an object. Therefore, we can conclude that mysqli->query() failed. It is possible to view the error logs of mysqli to debug further, but before that, an obvious error that stands out is that you have not told it the name of the database to run the query against

Change

$mysqli = mysqli_connect("localhost", "root", "Password");

To

$mysqli = mysqli_connect("localhost", "root", "Password", "databaseName");