0

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 another 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();
 }
}
Decent Dabbler
22.8k11 gold badges77 silver badges107 bronze badges
asked Aug 19, 2011 at 1:13
2

4 Answers 4

3

$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(); 
 } 
 } 
answered Aug 19, 2011 at 1:18
Sign up to request clarification or add additional context in comments.

Comments

1

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();
?>
answered Aug 19, 2011 at 1:32

Comments

1

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);
BenMorel
37k52 gold badges208 silver badges339 bronze badges
answered Aug 19, 2011 at 1:20

Comments

0

it turns out you need to do this

$this->openconn();

instead of just

openconn();
René Höhle
27.4k22 gold badges78 silver badges91 bronze badges
answered Aug 20, 2011 at 4:09

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.