<?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>Will Fitch&#039;s Blog &#187; database</title>
	<atom:link href="http://www.willfitch.com/tag/database/feed" rel="self" type="application/rss+xml" />
	<link>http://www.willfitch.com</link>
	<description></description>
	<lastBuildDate>Sun, 11 Dec 2011 23:39:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>MySQLi Tutorial</title>
		<link>http://www.willfitch.com/mysqli-tutorial.html</link>
		<comments>http://www.willfitch.com/mysqli-tutorial.html#comments</comments>
		<pubDate>Sun, 09 Jul 2006 22:55:32 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[mysqli]]></category>

		<guid isPermaLink="false">http://2131230664</guid>
		<description><![CDATA[Moving from a procedural system to object-oriented can be a daunting task. One feature to assist you is the MySQLi class, which allows for an object-oriented approach to database manipulation. This tutorial gives insight into the structure and basic usage of the MySQLi class. If PDO isn&#8217;t an option for you, then try MySQLi! MySQLi [...]]]></description>
			<content:encoded><![CDATA[<p>Moving from a procedural system to object-oriented can be a daunting task. One feature to assist you is the MySQLi class, which allows for an object-oriented approach to database manipulation. This tutorial gives insight into the structure and basic usage of the MySQLi class. If PDO isn&#8217;t an option for you, then try MySQLi!</p>
<p><span id="more-103"></span></p>
<p><strong><a href="http://us2.php.net/mysqli" target="_blank">MySQLi</a></strong> stands for MySQL Improved, and is available from MySQL versions >= 4.1.3 and must be compiled in PHP with &#8211;with-mysqli=/path/to/mysql/bin/mysql_config.</p>
<p>There are three hierarchical classes with MySQLi:</p>
<ol>
<li>mysqli &#8211; Represents a connection between PHP and a MySQL database</li>
<li>mysqli_stmt &#8211; Represents a prepared statement</li>
<li>mysqli_result &#8211; Represents the result set obtained from a query against the database</li>
</ol>
<p>Each one of these classes represent a different situation. The mysqli class is absolutely necessary because you will be communicating only through this class. It contains the connection to the other two classes (mysqli_stmt and _result). For instance, if you were executing a SELECT statement to pull cities from the states table, you would do something like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$mysqli</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> mysqli<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'hostname'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'database'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//This is where we will query the database and pull using the cities/states SELECT statement</span>
<span style="color: #666666; font-style: italic;">// If the result returns true</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mysqli</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT city FROM state WHERE state='AL'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// So the result returned true, let's loop and print out each city.</span>
<span style="color: #666666; font-style: italic;">// The number of rows returned is assigned to the property &quot;num_rows&quot; in the mysql_result class</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'For the state of AL, there are '</span><span style="color: #339933;">.</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">num_rows</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' cities.
'</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// The &quot;fetch_object()&quot; method is the equivalent of the old mysql_fetch_object() function. It allows access to the returned rows within the resouce object ($result in this case).</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch_object</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'City Name: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">city</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'
'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// Notice below that the errors are still contained within the mysqli class. This means that each result will affect a single &quot;error&quot; property. In otherwords, if your result fails, the error returned from MySQL is assigned to the property &quot;error&quot;.</span>
<span style="color: #666666; font-style: italic;">// This means the query failed</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$mysqli</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end else</span>
<span style="color: #000088;">$mysqli</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Before we go any further, let&#8217;s look at each one of the methods and parameters of the mysqli and mysqli_result class. This tutorial will not cover the mysql_stmt class, as this will be &#8220;Part 2&#8243;, which will cover prepared statements, and will be available within the next week.</p>
<p>Part 3 of the mysqli tutorial will cover transactions using the InnoDB engine.</p>
<p><strong>mysqli Class Methods:</strong></p>
<ul>
<li><strong>mysqli</strong> &#8211; construct a new mysqli object</li>
<li><strong>autocommit </strong>- turns on or off auto -commiting database modifications</li>
<li><strong>change_user </strong>- changes the user of the specified database connection</li>
<li><strong>character_set_name </strong>- returns the default character set for the database connection</li>
<li><strong>close </strong>- closes a previously opened connection</li>
<li><strong>commit </strong>- commits the current transaction</li>
<li><strong>connect </strong>- opens a new connection to MySQL database server</li>
<li><strong>debug </strong>- performs debugging operations</li>
<li><strong>dump_debug_info </strong>- dumps debug information</li>
<li><strong>get_client_info </strong>- returns client version</li>
<li><strong>get_host_info </strong>- returns type of connection used</li>
<li><strong>get_server_info </strong>- returns version of the MySQL server</li>
<li><strong>get_server_version </strong>- returns version of the MySQL server</li>
<li><strong>init </strong>- initializes mysqli object</li>
<li><strong>info </strong>- retrieves information about the most recently executed query</li>
<li><strong>kill </strong>- asks the server to kill a mysql thread</li>
<li><strong>multi_query </strong>- performs multiple queries</li>
<li><strong>more_results </strong>- check if more results exist from currently executed multi -query</li>
<li><strong>next_result </strong>- reads next result from currently executed multi -query</li>
<li><strong>options </strong>- set options</li>
<li><strong>ping </strong>- pings a server connection or reconnects if there is no connection</li>
<li><strong>prepare </strong>- prepares a SQL query</li>
<li><strong>query </strong>- performs a query</li>
<li><strong>real_connect </strong>- attempts to open a connection to MySQL database server</li>
<li><strong>escape_string </strong>- escapes special characters in a string for use in a SQL statement, taking into account the current charset of the connection</li>
<li><strong>rollback </strong>- rolls back the current transaction</li>
<li><strong>select_db </strong>- selects the default database</li>
<li><strong>set_charset </strong>- sets the default client character set</li>
<li><strong>ssl_set </strong>- sets ssl parameters</li>
<li><strong>stat </strong>- gets the current system status</li>
<li><strong>stmt_init</strong> &#8211; initializes a statement for use with mysqli_stmt_prepare</li>
<li><strong>store_result </strong>- transfers a resultset from last query</li>
<li><strong>thread_safe </strong>- returns whether thread safety is given or not</li>
<li><strong>use_result </strong>- transfers an unbuffered resultset from last query</li>
</ul>
<p><strong>mysqli Class properties:</strong></p>
<ul>
<li><strong>affected_rows </strong>- gets the number of affected rows in a previous MySQL operation</li>
<li><strong>client_info </strong>- returns the MySQL client version as a string</li>
<li><strong>client_version </strong>- returns the MySQL client version as an integer</li>
<li><strong>errno </strong>- returns the error code for the most recent function call</li>
<li><strong>error </strong>- returns the error string for the most recent function call</li>
<li><strong>field_count </strong>- returns the number of columns for the most recent query</li>
<li><strong>host_info </strong>- returns a string representing the type of connection used</li>
<li><strong>info </strong>- retrieves information about the most recently executed query</li>
<li><strong>insert_id </strong>- returns the auto generated id used in the last query</li>
<li><strong>protocol_version </strong>- returns the version of the MySQL protocol used</li>
<li><strong>server_info </strong>- returns a string that represents the server version number</li>
<li><strong>server_version </strong>- returns the version number of the server as an integer</li>
<li><strong>sqlstate </strong>- returns a string containing the SQLSTATE error code for the last error</li>
<li><strong>thread_id </strong>- returns the thread ID for the current connection</li>
<li><strong>warning_count </strong>- returns the number of warnings generated during execution of the previous SQL statement</li>
</ul>
<p>The best way to explain the usage of the mysqli class is to show by example. There are too many methods and properties to go over in this tutorial, so I will only give examples of some.</p>
<p>It&#8217;s important to note that some of these methods listed, aren&#8217;t methods at all. For instance, if you wanted to get the client library version of MySQL, you would use mysqli_get_client_version(), which returns the integer version. Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Client version: <span style="color: #009933; font-weight: bold;">%d</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">mysqli_get_client_version</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>You are forced to use the functions since there is no need for the connection to the actual database.</p>
<p>You have already seen the usage of the mysql_result connection from the mysqli class, so let&#8217;s go over some more examples:</p>
<p><strong>Getting some info on the result from the mysql database:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$mysqli</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> mysqli<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'db_user'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'my_password'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'mysql'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Get all the MySQL users and their hosts</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT user, host FROM user&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// If the query goes through</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mysqli</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// If there are some results</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">num_rows</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// Let's show some info</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'There were '</span><span style="color: #339933;">.</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">num_rows</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' rows returned.
'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'We selected '</span><span style="color: #339933;">.</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">field_count</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' fields in our query.
'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Our thread ID is '</span><span style="color: #339933;">.</span><span style="color: #000088;">$mysqli</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">thread_id</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'
'</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Ok, let's show the data</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch_object</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Username: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' :: Host: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">host</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'
'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end while loop</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'There are no results to display. Odd how we connected to this database and there are no users.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end else</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// Notice the error would be within the mysqli class, rather than mysql_result</span>
<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;There has been an error from MySQL: <span style="color: #009933; font-weight: bold;">%s</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$mysqli</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end else</span>
<span style="color: #666666; font-style: italic;">// Close the DB connection</span>
<span style="color: #000088;">$mysqli</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Here we used some common methods and properties to test and display information. Next week we will go over prepared statements and how they might assist in your scripts.</p>
<p>If you have any questions regarding MySQLi, please feel free to put comments and I will answer them soon.</p>
<p>&#8211; Will</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/mysqli-tutorial.html/feed</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
	</channel>
</rss>

