我试图在php中创建一个简单的搜索引擎,如果用户输入关键字,他们可以按事件名称或位置进行搜索。然后结果以日期顺序显示。大部分都来自另一个网站,但我试图转换它。
有人可以用简单的术语解释下面的错误给新手解释吗?
/home/ubuntu/workspace/test/test.php:49:array(5){' eventID' =>字符串(1)" 1" ' eventName的' => string(8)"令人兴奋的" ' eventLocation' => string(7)" Stadium" '开始' => string(10)" 2017-04-01" '到期' => string(10)" 2017-04-30"警告:非法字符串偏移' eventName'在/home/ubuntu/workspace/test/test.php第50行调用堆栈:0.0003 238624 1. {main}()/home/ubuntu/workspace/test/test.php:0 1
DisplayText
}
<?php
session_start();
//include files
include 'header/header.php';
include 'nav/navigation.php';
include 'init.php';
$expires = strtotime($_POST["expires"]);
$expires = date('Y-m-d', $expires);
$events = "";
$find = $_POST['find'];
$field = $_POST['field'];
$searching = true;
if(isset($_POST["submit"])) {
if(!empty($_POST["events"])) {
$searching = false;
}
//This is only displayed if the user submitted the form
if($searching == true)
{
echo "<h2>Results</h2><p>";
//If the user did not enter a search term, they receive an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
// Otherwise we connect to the database
//$result = mysqli_query($connection,$query) or exit ("Error in query:
$query. ".mysqli_error($connection));
// We preform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
//Now we search for our search term, in the field the user specified
$mysqli = new mysqli('localhost', 'root', '', 'c3470438');
$query = "SELECT * FROM events WHERE upper(eventLocation)
LIKE'%STADIUM%'";
$result = $mysqli->query($query);
//Temporarily echo $query for debugging purposes
//echo "$query";
//exit;
//And display the results
$row = $result->fetch_array(MYSQLI_ASSOC);
echo "<br>";
foreach($row as $item) {
var_dump($row);
echo $item['eventName'];
exit();
}
exit;
//This counts the number or results. If there aren't any, it gives an
explanation
$anymatches=mysql_num_rows($data);
if ($anymatches == 0) {
echo "Sorry, but we can not find an entry to match your query<br>
<br>";
}
//And reminds the user what they searched for
echo "<b>Searched For:</b> " .$find;
}
答案 0 :(得分:0)
好$result->fetch_array(MYSQLI_ASSOC)
返回一行,但您正在使用它,就像它返回所有行一样。所以你的foreach迭代行中的每个字段,而不是每行,因此你试图访问字符串的数组键。
像这样使用一段时间:
while($row = $result->fetch_array(MYSQLI_ASSOC)){
echo $row['eventName'];
}
此外,您使用的是不属于mysqli的mysql_num_rows
。而是使用$result->num_rows
。