在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");
你不能从构造函数中使用返回值......你也使用php4和php5风格的属性声明。此外您的查询是错误的。乍一看,你的代码可能有更多的错误。 – PeeHaa 2013-02-27 14:12:37
好吧,没有正确理解你的回应,好吧,我编辑我的代码,因为它应该是。仍然没有插入。正如我理解你的意思是关于var $ con和public $ coach的php4和php5样式属性声明。我在这里用错了是吗? – ArnasGo 2013-02-27 14:21:44
显示我们更新的代码 – Adder 2013-02-27 14:22:34