[ciapug] On or Off ...

Carl Olsen carl-olsen at mchsi.com
Mon Aug 15 21:11:05 CDT 2005


Here's a new version of my file with the prepared statements and all of the
function calls to mysqli written in OOP:

<?php

class faqs
{
	private $id;
	private $question;
	private $answer;
	private $order;
	private $conn;
	private $needsupdating = false;
	
	public function __construct($faqid)
	{
		$this->conn =  new mysqli("host", "user", "pswd", "db");
		if(mysqli_connect_errno())
		{
			throw new Exception("Unable to connect to the
server: " . mysqli_connect_error());
		}
		if($stmt = $this->conn->prepare("SELECT faq_question,
faq_answer, faq_order FROM faq_faq WHERE faq_id = ?"))
		{
			$stmt->bind_param("i", $faqid);
			$stmt->execute();
			$stmt->bind_result($faq_question, $faq_answer,
$faq_order);
			$stmt->fetch();
			$this->id = $faqid;
			$this->question = $faq_question;
			$this->answer = $faq_answer;
			$this->order = $faq_order;
			$stmt->close();
		}
	}
	
	public function getquestion()
	{
		return $this->question;
	}
	public function getanswer()
	{
		return $this->answer;
	}
	public function getorder()
	{
		return $this->order;
	}
	public function setquestion($question)
	{
		if(!is_string($question) || strlen($question) == 0)
		{
			throw new Exception("Invalid question value");
		}
		$this->question = $question;
		$this->needsupdating = true;
	}
	public function setanswer($answer)
	{
		if(!is_string($answer) || strlen($answer) == 0)
		{
			throw new Exception("Invalid answer value");
		}
		$this->answer = $answer;
		$this->needsupdating = true;
	}
	public function setorder($order)
	{
		if(!is_integer($order) || strlen($order) == 0)
		{
			throw new Exception("Invalid order value");
		}
		$this->order = $order;
		$this->needsupdating = true;
	}
	
	public function __destruct()
	{
		if(!$this->needsupdating)
		{
			return;
		}
		if ($stmt = $this->conn->prepare("UPDATE faq_faq SET
faq_question = ?, faq_answer = ?, faq_order = ? WHERE faq_id = ?"));
		{
			$stmt->bind_param("ssii", $faq_question,
$faq_answer, $faq_order, $faq_id);
			$faq_question = $this->question;
			$faq_answer = $this->answer;
			$faq_order = $this->order;
			$faq_id = $this->id;
			$stmt->execute();
			$stmt->close();
		}
		$this->conn->close();
	}
}

?>

-----Original Message-----
From: ciapug-bounces at cialug.org [mailto:ciapug-bounces at cialug.org] On Behalf
Of Carl Olsen
Sent: Friday, August 12, 2005 6:24 PM
To: 'Tony Bibbs'; ciapug at cialug.org
Subject: RE: [ciapug] On or Off ...

As you can see from the example I just sent, I'm using mysqli without the
prepared statements and I'm using the database abstraction layer to escape
the arguments.  What am I going to gain by switching to prepared statements?

I can see the code might be fewer lines, but I'm also wondering about
performance.  Am I going to realize an improvement in performance?

Thank for your help!

Carl
http://www.carl-olsen.com/




More information about the ciapug mailing list