<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Weblimner &#187; function</title>
	<atom:link href="http://www.weblimner.com/tag/function/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.weblimner.com</link>
	<description></description>
	<lastBuildDate>Sun, 15 Aug 2010 23:49:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Using MySQLi in a Seperate Class</title>
		<link>http://www.weblimner.com/tutorial/using-mysqli-in-a-seperate-class/</link>
		<comments>http://www.weblimner.com/tutorial/using-mysqli-in-a-seperate-class/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 21:57:39 +0000</pubDate>
		<dc:creator>Ali Sipahioglu</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[constructor]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[destructor]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[localhost]]></category>
		<category><![CDATA[mysqli]]></category>

		<guid isPermaLink="false">http://www.weblimner.com/?p=155</guid>
		<description><![CDATA[If you have ever used OOP in your programming, I am sure you have experienced the flexibility you get. Also with the introduction of MySQLi class, we did not need to create our own database class anymore since MySQLi comes with almost all the functions you might need. Now the scope of this article is [...]]]></description>
			<content:encoded><![CDATA[<p>If you have ever used OOP in your programming, I am sure you have experienced the flexibility you get. Also with the introduction of MySQLi class, we did not need to create our own database class anymore since MySQLi comes with almost all the functions you might need. Now the scope of this article is to show you use MySQLi in a class you created that handles something else but still needs a database connection and it is just redundant to create a new connection for every class you have in your code. <span id="more-155"></span><br />
<br/><br/><br />
<strong>Note:</strong> Explaining MySqli functions and creating classes are not in the scope of this article and you need to have a good level of PHP knowledge.<br />
<br/><br/></p>
<h3>Databse Connection</h3>
<p>Lets say you have a file named <strong>db.php</strong> in your file directory. This is all you need in there unless you created a different class that extends the MySQLi :)</p>
<pre class="php">
&lt;?php
	$mysqli = new mysqli('localhost','username','password','database');
?&gt;
</pre>
<p><br/><br/><br />
It basically sets &#8220;$mysqli&#8221; variable to the connection of your database. This is the beauty of mysqli, all you need to do is just set your connection and you have your database class.<br />
<br/><br/></p>
<h3>Class</h3>
<p>Next up is your class. Lets say you have a class named books which gets the records for a book from a table called books from your database.<br />
I saved this file as <strong>book.php</strong> in my file directory<br />
<br/><br/></p>
<pre class="php">
&lt;?php
class books{
	public $error;
	function __construct($mysqli){}

	function get_book($id){
		global $mysqli;
		if($result = $mysqli->query("SELECT* from books where book_id='$id'")){
			$row=$result->fetch_object();
			return $row;
		}else{
			$this->error = $mysqli->error;
		}

	}
}
?&gt;
</pre>
<p><br/><br/><br />
Now to explain the code;</p>
<h4>Constructor</h4>
<p>Following line requires you to send the mysqli connection you created earlier in your db.php. If you want you can have more in your constructor class obviously but in this example I didn&#8217;t need to put anything in there. <a href="http://php.net/manual/en/language.oop5.decon.php" target="_blank">More on Constructors and Destructors</a></p>
<pre class="php">
	function __construct($mysqli){}
</pre>
<p><br/><br/></p>
<h4>get_book function:</h4>
<p>You need to set your $mysqli variable to be global so you can use it in the function. You accomplish that by just &#8220;global $mysqli;&#8221; line. If you don&#8217;t need a database connection in the function you do not need to set this.<br />
<br/><br/><br />
The rest of the function is just basic querying and getting results.<br />
<br/><br/></p>
<h3>Using the class</h3>
<p>In your <strong>index.php</strong> include both of these files and then assign your class to a variable to be accessed. Notice that we are sending in the $mysqli variable in to the class.</p>
<pre class="php">

include "db.php";
include "book.php";
$my_book_class = new books($mysqli);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.weblimner.com/tutorial/using-mysqli-in-a-seperate-class/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)
Object Caching 257/260 objects using disk

Served from: www.weblimner.com @ 2012-02-06 03:28:26 -->
