Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在函数之间传递db连接_Php_Mysql - Fatal编程技术网

Php 在函数之间传递db连接

Php 在函数之间传递db连接,php,mysql,Php,Mysql,我对PHP相当陌生,所以这可能是一个简单的问题。这是我用来更新数据库的类。问题是它在标记为*的行中不断给我一个错误,因为它找不到$con,这显然在函数openconn()中。似乎我无法将连接传递到另一个函数。我做错什么了吗?谢谢 class retreats { public $retreat_name = ''; function openconn() { $con = mysql_connect("localhost","root","root");

我对PHP相当陌生,所以这可能是一个简单的问题。这是我用来更新数据库的类。问题是它在标记为*的行中不断给我一个错误,因为它找不到$con,这显然在函数openconn()中。似乎我无法将连接传递到另一个函数。我做错什么了吗?谢谢

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,我建议你仔细阅读并感谢..我会试试这个.谢谢..我会试试这个。