Php 在函数之间传递db连接
我对PHP相当陌生,所以这可能是一个简单的问题。这是我用来更新数据库的类。问题是它在标记为*的行中不断给我一个错误,因为它找不到$con,这显然在函数openconn()中。似乎我无法将连接传递到另一个函数。我做错什么了吗?谢谢Php 在函数之间传递db连接,php,mysql,Php,Mysql,我对PHP相当陌生,所以这可能是一个简单的问题。这是我用来更新数据库的类。问题是它在标记为*的行中不断给我一个错误,因为它找不到$con,这显然在函数openconn()中。似乎我无法将连接传递到另一个函数。我做错什么了吗?谢谢 class retreats { public $retreat_name = ''; function openconn() { $con = mysql_connect("localhost","root","root");
class retreats {
public $retreat_name = '';
function openconn() {
$con = mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("PHPTest", $con);
}
function closeconn(){
mysql_close($con);
}
function add_retreat(){
openconn();
$sql="INSERT INTO tbl_retreats (retreat_name) VALUES ('".$this->retreat_name."')";
if (!mysql_query($sql,$con)) *******
{
die('Error: ' . mysql_error());
}
echo "Record Successfully Added";
closeconn();
}
}
$con
是函数openconn
的局部变量。尝试以以下方式更改代码:
class retreats {
public $retreat_name = '';
private $con;
function openconn() {
$this->con = mysql_connect("localhost","root","root");
if (!$this->con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("PHPTest", $this->con);
}
function closeconn(){
mysql_close($this->con);
}
function add_retreat(){
openconn();
$sql="INSERT INTO tbl_retreats (retreat_name) VALUES ('".$this->retreat_name."')";
if (!mysql_query($sql,$this->con))
{
die('Error: ' . mysql_error());
}
echo "Record Successfully Added";
closeconn();
}
}
$con
是函数openconn
的局部变量。尝试以以下方式更改代码:
class retreats {
public $retreat_name = '';
private $con;
function openconn() {
$this->con = mysql_connect("localhost","root","root");
if (!$this->con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("PHPTest", $this->con);
}
function closeconn(){
mysql_close($this->con);
}
function add_retreat(){
openconn();
$sql="INSERT INTO tbl_retreats (retreat_name) VALUES ('".$this->retreat_name."')";
if (!mysql_query($sql,$this->con))
{
die('Error: ' . mysql_error());
}
echo "Record Successfully Added";
closeconn();
}
}
您需要首先在类中声明
$con
。只要把它放在public$return\u name=''后面代码>
放
之后,您可以使用$this
关键字在其他函数中使用它
mysql_close($this->con);
您需要首先在类中声明$con
。只要把它放在public$return\u name=''后面代码>
放
之后,您可以使用$this
关键字在其他函数中使用它
mysql_close($this->con);
一个简单的PDO端口的代码
<?php
class pdoDB{
//PDO Connect
function connect($host,$db,$user,$pass){
$this->dbh = new PDO('mysql:host='.$host.';dbname='.$db, $user, $pass);
}
function query($query){
$this->retreat_name = $query;
$this->prepare();
}
function prepare(){
/* Execute a prepared statement by binding PHP variables */
$this->sth = $this->dbh->prepare('INSERT INTO tbl_retreats (retreat_name) VALUES (:value)');
$this->sth->bindParam(':value', $this->retreat_name);
$this->execute();
}
function execute(){
$this->sth->execute();
}
function result(){
if ($this->sth->rowCount() > 0) {
return 'Record Successfully Added';
}else{
return 'Record Not Inserted';
}
}
function close(){
$this->sth = null;
}
}
$db = new pdoDB();
$db->connect('localhost','PHPTest','root','pass');
$db->query('Barcelona'); //or $db->query($_POST['retreat_name']);
echo $db->result();
$db->close();
?>
代码的简单PDO端口
<?php
class pdoDB{
//PDO Connect
function connect($host,$db,$user,$pass){
$this->dbh = new PDO('mysql:host='.$host.';dbname='.$db, $user, $pass);
}
function query($query){
$this->retreat_name = $query;
$this->prepare();
}
function prepare(){
/* Execute a prepared statement by binding PHP variables */
$this->sth = $this->dbh->prepare('INSERT INTO tbl_retreats (retreat_name) VALUES (:value)');
$this->sth->bindParam(':value', $this->retreat_name);
$this->execute();
}
function execute(){
$this->sth->execute();
}
function result(){
if ($this->sth->rowCount() > 0) {
return 'Record Successfully Added';
}else{
return 'Record Not Inserted';
}
}
function close(){
$this->sth = null;
}
}
$db = new pdoDB();
$db->connect('localhost','PHPTest','root','pass');
$db->query('Barcelona'); //or $db->query($_POST['retreat_name']);
echo $db->result();
$db->close();
?>
事实证明你需要这样做
$this->openconn();
而不仅仅是
openconn();
事实证明你需要这样做
$this->openconn();
而不仅仅是
openconn();
您没有返回连接处理程序$con
来自openconn()
Allen,我建议您仔细阅读,不要返回连接处理程序$con
来自openconn()
Allen,我建议你仔细阅读并感谢..我会试试这个.谢谢..我会试试这个。