2013-02-27 76 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