我知道这个问题已被多次询问,但是我仍然无法让这个问题起作用!!!
我正在尝试创建一个基本的搜索引擎,我在phpMyadmin mysql数据库中手动输入页面等。不断出现的错误是:
致命错误:在第43行的C:\ xampp \ htdocs \ projectname \ searchit.php中的非对象上调用成员函数mysqli_fetch_array()
所以这是代码!!!
<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>
答案 0 :(得分:1)
您需要修复以下内容:
$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)
最后:
$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'];
}
答案 1 :(得分:0)
此错误消息表示您尝试在对象上调用方法,该对象实际上不是对象。这通常意味着,就像你的情况一样,它应该是一个对象,但不知何故,它实际上从未实现过。在你的情况下,它应该成为一个对象
$result = mysqli->query($query);
如果一切按计划进行,$result
将成为一个对象,可以访问mysqli_fetch_array()
方法。但是,您的错误消息意味着$result
永远不会成为对象。因此,我们可以得出结论mysqli->query()
失败了。可以查看mysqli的错误日志以进一步调试,但在此之前,一个明显的错误是突出的是你没有告诉它运行查询的数据库的名称
更改
$mysqli = mysqli_connect("localhost", "root", "Password");
要
$mysqli = mysqli_connect("localhost", "root", "Password", "databaseName");