PHP和mysql连接 [英] PHP and mysql connections
本文介绍了PHP和mysql连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在PHP类文件中创建连接并在类中的所有不同方法中使用它?我试图在构造函数中打开一个连接,我得到一个错误,当我得到关闭连接方法说,我在mysql_close()语句中提供的参数不是一个有效的MYSQL链接的源。 p>
更新:Ok我想出了一个变量拼写错误。
解决方案
这是完全可能的,你只需要使数据库链接一个类成员:
class MyDBClass {
var $ sth;
function __construct($ host,$ user,$ pass){
$ this-> sth = mysql_connect($ host,$ user,$ pass);
}
function close(){
mysql_close($ this-> sth);
}
}
Is it possible to create a connection in a PHP class file and use it in all of the different methods in the class? I am trying to open a connection in the constructor and i get an error when i get to the close connection method saying that the argument that I've provided in the mysql_close() statement isn't a valid MYSQL-Link souce.
Update: Ok I figured it out I had a variable misspelled.
解决方案
It is entirely possible, you just need to make the database link a class member:
class MyDBClass {
var $sth;
function __construct($host, $user, $pass) {
$this->sth = mysql_connect($host, $user, $pass);
}
function close() {
mysql_close($this->sth);
}
}
这篇关于PHP和mysql连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文