2011-08-19 5 views
0

저는 PHP에서 매우 새롭기 때문에 간단한 질문입니다. 이것은 데이터베이스를 업데이트하는 데 사용하는 클래스입니다. 문제는 openconn() 함수에 분명히있는 $ con을 찾을 수 없기 때문에 *으로 표시된 줄에 오류가 계속 발생한다는 것입니다. 다른 함수에 연결을 전달할 수없는 것 같습니다. 내가 뭔가 잘못하고 있습니까? 감사함수 들간의 db 연결 전달하기

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

    } 
} 
+0

당신의 대신이

$this->openconn(); 

을하지 할 필요가 밝혀 연결 처리기를 반환합니다. 'openconn() '$ con' –

+2

Allen, [Object Oriented Programming (OOP)의 기초] (http://www.php.net/manual/en/language.oop5)를 읽어 보시기 바랍니다. basic.php) 및 [properties] (http://www.php.net/manual/en/language.oop5.properties.php) –

답변

3

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

    }  

    } 
+0

감사합니다. – Allen

1

먼저 클래스에 $con을 선언해야합니다. 그냥 public $retreat_name = '';

후 넣어 당신은 $this 키워드를 사용하여 다른 기능에서 사용할 수 있습니다, 그 후

public $retreat_name = ''; 
private $con; 

을 넣어.

mysql_close($this->con); 
1

코드의 간단한 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(); 
?> 
0

당신이 단지

openconn();