2016-04-08 107 views
0

你好,我目前正试图连接我的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语句,我现在得到的是

$CalendarDetails->bind_param("s", $month); 
线26类似的错误

我现在收到的错误是: 调用一个非对象的成员函数bind_param()

更新3 我的准备语句失败的原因是由于拼写错误,我的数据库中的日历拼写错误。

+2

'$ event [$ j] = $ date +“|” + ......'那些应该是点,而不是加号。 –

+1

“来自20号线” - 什么是20号线? – FuzzyTree

+0

谢谢你,我会改变这一点,混合php与Java连接哈哈。 – noob

回答

0

导致错误的代码位是:$CalendarDetails->error。根据你的逻辑,唯一的方法就是满足这个条件:if (false===$CalendarDetails)。这意味着$CalendarDetailsfalsefalse是一个布尔值,而不是一个对象。因此,当您执行$CalendarDetails->error(有效地将其转换为false->error)时,您试图获取非对象(false)的属性(error)。

+0

没有我的逻辑基于这样一个事实,即php上的变量是动态的,换句话说,您不需要声明它们的类型,所以我假设如果查询失败,它会输出布尔值而不是对象。此外,我不知道如何错误检查在PHP这只是我发现为其他人工作。我以前在其他php文件中使用过这种方法(如果你想要的话,文件的例子可用),它似乎从来没有进入if语句,所以我认为它的工作。 – noob

+0

无论如何,我会将它们从我的文件中删除,但现在虽然我想知道为什么它在特定情况下输入if语句,而不是在其他情况下几乎执行相同操作。 – noob

+0

将此标记为可能有类似问题的人的正确答案。虽然我的文件没有什么错(为什么准备声明失败),这是我真正问到的问题(由于我自己的无知哈)你解释了这个问题非常感谢。 – noob