2012-03-15 3 views
2

これは私の最初の投稿ですので、私には簡単に行く^ _^致命的なエラー:非オブジェクトのメンバ関数close()を呼び出します。 MySQLiの問題

私はライブサーバーにアップロードしたときに次のエラーが発生しています。それは私が奇妙だと思ったlocalhost上で動作します。どんな助けでも大歓迎です。

Fatal error: Call to a member function close() on a non-object.... 

$stmt->close(); 

DB

$connection=new mysqli($MYSQL_HOST,$MYSQL_USER,$MYSQL_PASS,$DB)or die(mysqli_error($connection)); 

クラス自体への接続を指すライン。

function getTimes(){ //this method just pulls the results of the query and returns them as an array 
     global $connection; 
     $route = $this->route; 
     $station = $this->station; 
     $day = $this->day; 

     // create a prepared statement 
     if ($stmt = $connection->prepare("select time from timetable where route=? and day=? and station=?")) { 
      $stmt->bind_param("sss", $route, $day, $station); // bind parameters for markers 
      $stmt->execute(); //execute query    
      $stmt->bind_result($col1); //bind result variables 
      while ($stmt->fetch()){ 
       $results[]=$col1; 
      } 
     } 
     $stmt->close();//close statement 
     return $results; 
    } 
+1

何らかの理由で$ connection-> prepare()が失敗したようです。 falseを返した場合、$ stmtはfalseで、期待通りのmysqliオブジェクトではありません。 – Crashspeeder

+0

あなたの$接続もnullで、あなたがそれをチェックしていない可能性もあります。しかし、それは現時点であなたのエラーではありません – Churk

+1

@Churkこれは、致命的なエラーになります:非オブジェクト上のメンバ関数prepare()を呼び出します。どちらの方法でも、私は接続としてパラメータとして渡す必要があることに同意します: 'function getTimes(mysqli $ connection){}' – cmbuckley

答えて

3

あなたはif節に$stmtを入れる必要があります。あなたは$stmt->close();を受け取ります$stmt->close();

2

あなたの問題は、$stmtオブジェクトがif条件テストの一部としてインスタンス化されたということでした。失敗した場合、つまりfalseを返す場合は、とにかく->close()に電話をかけようとしていました。私はifブロック内でメソッド呼び出しを移動しました。

else句を追加する必要があります。これは、スクリプトが文を準備できなかったことを処理するためで、ローカルでは動作しますがライブサーバでは動作しないと言えば、ここで問題を引き起こす。 display_errors('1')error_reporting(E_ALL)でエラー処理を有効にする必要があります。あなたの新しい脚本を世界に向けさせる前に、これらをオフにすることを忘れないでください。 :)

function getTimes(){ //this method just pulls the results of the query and returns them as an array 
     global $connection; 
     $route = $this->route; 
     $station = $this->station; 
     $day = $this->day; 

     // create a prepared statement 
     if ($stmt = $connection->prepare("select time from timetable where route=? and day=? and station=?")) { 
      $stmt->bind_param("sss", $route, $day, $station); // bind parameters for markers 
      $stmt->execute(); //execute query    
      $stmt->bind_result($col1); //bind result variables 
      while ($stmt->fetch()){ 
       $results[]=$col1; 
      } 
      $stmt->close();//close statement 
     } 

     return $results; 
    } 
+1

+1 else節で '$ connection-> error'を使ったエラー処理は、このシナリオをデバッグするのに役立ちます。 – cmbuckley

+0

ありがとう、一度私はclose()をif文に移動した後、それはうまくいきました。私は、問題に帰着していた時点で情報がないテーブルの情報を引き出そうとしていたことが分かりました。 – cosmicsafari

2

$stmtが正常に作成された場合にのみ呼び出されるように、close()呼び出しをifステートメントに移動します。私はあなたがエラーを持っていないローカルマシン上の疑い

// create a prepared statement 
    if ($stmt = $connection->prepare("select time from timetable where route=? and day=? and station=?")) { 
     $stmt->bind_param("sss", $route, $day, $station); // bind parameters for markers 
     $stmt->execute(); //execute query    
     $stmt->bind_result($col1); //bind result variables 
     while ($stmt->fetch()){ 
      $results[]=$col1; 
     } 
     $stmt->close();//close statement 
    } 

はオンになっていますが、にアップロードするサーバー上で、エラーがでています。

ここで扱う根本的な問題は、prepare()が失敗しているという事実です。そこにある問題は、サーバー上のデータベースにtimetableテーブルがないか、テーブルに1つまたは複数のフィールドがありません。routedayまたはstationです。 cbuckleyが言ったように、完全なエラーメッセージについては$connection->errorをチェックしてください。

サーバーにアップロードしたときに、サーバー上のデータベース構造の変更も忘れていませんか?

関連する問題