开发者

Passing db connection between functions

开发者 https://www.devze.com 2023-03-28 23:01 出处:网络
I\'m fairly new at PHP and so maybe this is a simple question.This is a class I use to update a database. The problem is that it keeps giving me an error at the line marked * because it can\'t find $c

I'm fairly new at PHP and so maybe this is a simple question. This is a class I use to update a database. The problem is that it keeps giving me an error at the line marked * because it can't find $con, which is clearly in the function openconn(). It seems I can't pass the connection to anothe开发者_如何转开发r function. Am I doing something wrong?Thanks

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 is a local variable for the function openconn. Try to change your code in this way:

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();  

    }     

   }  


A Simple PDO port of your code...

<?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();
?>


You need to first declare the $con in the class. Just put it after the public $retreat_name = '';

put

public $retreat_name = ''; 
private $con;

after that, you can use it in other functions using the $this keyword.

mysql_close($this->con);


it turns out you need to do this

$this->openconn();

instead of just

openconn();
0

精彩评论

暂无评论...
验证码 换一张
取 消