2012-02-29 88 views
4

出於某種原因,我的else語句沒有觸發,我認爲我的方法由於某種原因沒有返回true。返回true不返回或我的其他表述不起作用?

主代碼

function msg($content) 
{ 
    //function that echos out page with content in it 
} 

$car=new yamiko_car; 
if(!$car->add())//returns bool false on fail and true on success 
{msg($car->error);} 
else{msg('Car has been added');} 

,並沒有也返回上失敗錯誤在那裏我測試數據,但只是把評論有方法。已經測試過它,它工作正常。

public function add() 
{ 
    //gets data succesfully 

    //MySQL 
    $this->query("INSERT INTO car (year, make, model, price, obo, img1, img2, img3, img4, description) 
    VALUES ('$year', '$make', '$model', '$price', '$obo', '$img1', '$img2', '$img3', '$img4', 'txt') "); 

    echo 'mysql added'; 
    return true; 
} 

function query($sql, $result=false) 
{ 
    $query=mysql_query($sql); 
    if(!$query){$this->mysqlError.=mysql_error().'<br />';return false;} 
    if($result==false){return true;} 
    else{return $query;} 
} 

我加echo語句,並觸發精...
時,有它返回false錯誤和運行msg()
我切換語句if($car->add()){msg('car added')}else{msg($car->error);}無論是如果或else語句會觸發
此外,if語句之後的任何代碼都不會觸發,並且不存在php或mysql錯誤。

+1

你的附加功能返回TRUE每次 – 2012-02-29 13:11:49

+0

'回報BOOL FALSE的失敗,真正的success'我沒有看到當它可能返回FALSE – 2012-02-29 13:12:09

+0

不,它不會將註釋放在那裏,而不是顯示獲取和測試數據的所有代碼。 – Yamiko 2012-02-29 13:13:26

回答

-1

我想你錯過了括號:

$car=new yamiko_car; 

應該

$car=new yamiko_car(); 
+1

我會認爲他們沒有必要 – 2012-02-29 13:14:35

+1

沒有必要,如果是這樣,我的代碼會打破方法之一之前 – Yamiko 2012-02-29 13:16:57

+0

好吧,我認爲他們是必要的 - 學習永遠不會停止。抱歉! – acme 2012-02-29 13:24:02

1

聽起來也許PHP默默崩潰,並解釋只是停止。嘗試PHP error level設置爲這樣的事情(在PHP腳本的開始):

error_reporting(E_ALL); 
+0

所有即將到來的是,我已經開始了一個會議。 – Yamiko 2012-02-29 13:19:31

+0

嗯,嘗試添加這條線作爲PHP將解釋的第一行。例如,如果您包含'header.php',則需要將該行放在該文件的頂部。但是,如果所有的PHP代碼都在同一個文件中,那麼將該行放在第一個「<?php」行下面就足夠了。 – Christoffer 2012-02-29 13:21:45

+0

這就是我所做的。 – Yamiko 2012-02-29 13:22:50