2013-02-27 73 views
-1

在oop php中,我創建了構造函數mysql連接(我知道它將被棄用,並且你sugest使用PDO等),但我遇到了問題。連接完成後,一切都會好起來的。但插入無法完成不知道爲什麼,代碼運行到結束。看起來對象不接受連接,但它不可能。我使用PHP 5.4.3。代碼如下:在啓動對象後PHP OOP連接mysql不起作用

Table (Coach): 
Coach_id INT (AutoIncrement) 
Coach_name char(30) 
Coach_nationality char(30) 


class League 
{ 
    public $con; 

    public function MySQLCon() 
    { 
    $this->con = mysql_connect("localhost","root","") or mysql_error($this->con); 
    mysql_select_db("basket",$this->con) or mysql_error($this->con); 
    return $this->con; 
    } 

    public $coach,$coachNationality; 

    public function NewCoach($coach,$coachNationality) 
    { 

     $this->coach = $coach; 
     $this->coachNationality = $coachNationality; 

     $Query = "insert into Coach_name (Coach_name,Coach_nationality) VALUES ('".$this->coach."','".$this->coachNationality."')"; 

     //this query doesn't do anything but prints yes 
     mysql_query($Query,$this->con) or mysql_error($this->con); 
     echo "yes"; 

    } 
} 

//no data about mike Brown in database, database engine InnoDB 
$LG = new League; 
$LG->MySQLCon(); 
$LG->NewCoach("Mike Brown","USA"); 
+0

你不能從構造函數中使用返回值......你也使用php4和php5風格的屬性聲明。此外您的查詢是錯誤的。乍一看,你的代碼可能有更多的錯誤。 – PeeHaa 2013-02-27 14:12:37

+0

好吧,沒有正確理解你的迴應,好吧,我編輯我的代碼,因爲它應該是。仍然沒有插入。正如我理解你的意思是關於var $ con和public $ coach的php4和php5樣式屬性聲明。我在這裏用錯了是嗎? – ArnasGo 2013-02-27 14:21:44

+0

顯示我們更新的代碼 – Adder 2013-02-27 14:22:34

回答

1

首先STA rt使用錯誤消息:

class League 
{ 
    var $con; 

    public function __construct() 
    { 
    $this->con = mysql_connect("localhost","root","") or die("No connection: " . mysql_error()); 
    mysql_select_db("basket",$this->con) or die("Database could not be selected: " . mysql_error($this->con)); 
    } 

    public $coach,$coachNationality; 

    public function NewCoach($coach,$coachNationality) 
    { 

     $this->coach = $coach; 
     $this->coachNationality = $coachNationality; 

     $Query = "insert into Coach_name (Coach_name,Coach_nationality) VALUES ('".$this->coach."','".$this->coachNationality."')"; 

     //this query doesn't do anything but prints yes 
     mysql_query($Query,$this->con) or die(mysql_error($this->con)); 
     return true;  
    } 
} 

//no data about mike Brown in database, database engine InnoDB 
$LG = new League; 
if($LG->NewCoach("Mike Brown","USA")) echo "inserted, method NewCoach returned true"; 

編輯完代碼後;

  1. mysql_error將收到的唯一參數是連接,而不是字符串。
  2. 插入和選擇中的字符串需要用引號「或」包圍。
  3. mysql_query的第二個參數應該是連接
  4. 開始使用PDO或mysqli而不是mysql,因爲它將在未來版本中從PHP中刪除,並且已被認爲是反向練習和過時。
+0

感謝您的回答。我的代碼像您所顯示的編輯,但仍然沒有在數據庫中插入... – ArnasGo 2013-02-27 14:29:41

+0

沒有錯誤?能夠完成錯誤報告?頁面加載500嗎?你檢查了日誌文件嗎? – Luceos 2013-02-27 14:33:07

+0

沒有錯誤...是啓用: error_reporting(「E_ALL」); ini_set('dispaly_errors',true); – ArnasGo 2013-02-27 14:34:01

0

查詢是錯誤的,你需要使用單引號的字符串:

"insert into Coach_name (Coach_name,Coach_nationality) VALUES ('".$this->coach."','".$coachNationality."')"; 

更妙的是,使字符串中確保單qoutes被轉義,像這樣的:

... VALUES ('".mysql_real_escape_string($this->coach)."', .. 

,但你的代碼是如此怪異,可能會有更多的錯誤

+0

仍然無效。不知道爲什麼。替換查詢中仍然沒有數據。代碼是如此奇怪?我在做項目,但查詢仍然無法正常工作。 – ArnasGo 2013-02-27 14:14:44

+0

看看peehaa的評論,看看爲什麼你的代碼很奇怪(還有更多) – x4rf41 2013-02-27 14:15:52