你好,我目前正在尝试将我的php文件连接到数据库,但由于某种原因,我一直收到这个错误。我正在尝试获取非对象的属性。这似乎是来自第23行的查询本身的问题,但我一辈子都看不到这个问题。这是我的代码,任何帮助都将不胜感激。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include("dbconnect.php");
//get first 3 months
$month = '%'.$_POST['month'].'%';
//month 1 and 2 are currently not in use till issue is fixed
$month2 = '%'.$_POST['month'].'%';
$month3 = '%'.$_POST['month'].'%';
echo $month;
//connect to database and prepare mysqli statement to get day, title, details, time and location if month has any details
//will be adding more details for this statement to get once it is fully tested
if($connect->connect_errno){
printf("connection failed, please try again and if this error occurs again please submit this bug through the contact us page", $connect->connect_error);
exit();
}
$CalendarDetails = $connect->prepare("SELECT `date`, `time`, `title`, `details`, `eventLocation`, `longitude`, `latitude` FROM `calendar` WHERE `date` LIKE ?");
//$CalendarDetails = $connect->prepare("SELECT date, time, title, details, eventLocation, longitude, latitude FROM calendar WHERE date LIKE ?");
//check if connection succeeded
//if ( false===$CalendarDetails ) {
//error occurs in line below
// die('failed error 1 ' . htmlspecialchars($CalendarDetails->error));
//}
//bind the month to the prepared statement
$CalendarDetails->bind_param("s", $month);
//check if failed
//if ( false===$CalendarDetails ) {
// die('failed error 2 ' . htmlspecialchars($CalendarDetails->error));
//}
//execute query
$CalendarDetails->execute();
//check if failed
//if ( false===$CalendarDetails ) {
// die('failed error 3 ' . htmlspecialchars($CalendarDetails->error));
//}
//store results
$CalendarDetails->store_result();
//if ( false===$CalendarDetails ) {
// die('failed error 4 ' . htmlspecialchars($CalendarDetails->error));
//}
//bind results to separate variables
$CalendarDetails->bind_result($date, $time, $title, $details, $time, $location, $longitude, $latitude);
//get results
if($day != null){
if($month != null){
//create an array to store results and send to app
//$Allevents = array();
$i = 0;
$event[$i] = array();
while($CalendarDetails->fetch()){
$j = strval($i);
//this will combine all results into one string and will be split into an array in java
$event[$j] = $date. "|". $title. "|". $details. "|". $time. "|". $location. "|". $longitude. "|". $latitude;
/*$event['day'][$i] = $day;
$event['title'][$i] = $title;
$event['details'][$i] = $details;
$event['time'][$i] = $time;
$event['location'][$i] = $location;
$event['longitude'][$i] = $longitude;
$event['latitude'][$i] = $latitude;*/
//$Allevents[$i] = $event;
$i++;
}
echo json_encode($event);
mysqli_stmt_close($CalendarDetails);
}else{
echo "month not sent from app";
}
}else{
echo "no events this month";
}
?>
使用dbconnect文件代码更新
$connect= new mysqli("127.0.0.1",$config['username'],$password,$config['dbname'], 3306);
更新2我已经注释掉了与查询相关的if语句,现在我在第26行收到了一个类似的错误,即
$CalendarDetails->bind_param("s", $month);
我现在收到的错误是:调用非对象上的成员函数bind_param()
更新3我的准备语句失败的原因是拼写错误,我数据库中的日历拼写错误。
导致错误的代码位是:$CalendarDetails->error
。根据您的逻辑,实现这一点的唯一方法是满足以下条件:if ( false===$CalendarDetails )
。这意味着$CalendarDetails
是false
。false
是一个布尔值,而不是一个对象。因此,当您执行$CalendarDetails->error
(它实际上被转换为false->error
)时,您正试图获取非对象(false
)的属性(error
)。