试图获得非对象的属性,准备好的声明

时间:2016-04-08 18:48:34

标签: php sql prepared-statement

您好我正在尝试将我的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 我的准备语句失败的原因是由于拼写错误,我的数据库中的日历拼写错误。

1 个答案:

答案 0 :(得分:0)

导致错误的代码位是:$CalendarDetails->error。根据你的逻辑,唯一能达到这个目的的方法就是满足这个条件:if ( false===$CalendarDetails )。这意味着$CalendarDetails falsefalse是布尔值,而不是对象。因此,当您$CalendarDetails->error(实际上已转换为false->error)时,您正在尝试获取非对象(error)的属性(false)。 / p>