<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Programming Corner</title>
	<atom:link href="http://programmingcorner.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingcorner.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Tue, 21 Jul 2009 07:26:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='programmingcorner.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Programming Corner</title>
		<link>http://programmingcorner.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://programmingcorner.wordpress.com/osd.xml" title="Programming Corner" />
	<atom:link rel='hub' href='http://programmingcorner.wordpress.com/?pushpress=hub'/>
		<item>
		<title>MySQLi Prepared Statements</title>
		<link>http://programmingcorner.wordpress.com/2009/07/21/mysqli-prepared-statements/</link>
		<comments>http://programmingcorner.wordpress.com/2009/07/21/mysqli-prepared-statements/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 07:24:51 +0000</pubDate>
		<dc:creator>mucchuaonline</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://programmingcorner.wordpress.com/?p=5</guid>
		<description><![CDATA[Introduction This tutorial will guide you through creating MySQLi prepared statements. MySQLi is an extension/API for PHP that is also know as MySQL Improved. MySQLi is included with versions 5 of PHP and later that allows PHP developers to take advantage of all the features in MySQL 4.1.3. According to the manual: Quote: If you [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingcorner.wordpress.com&amp;blog=7638624&amp;post=5&amp;subd=programmingcorner&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:medium;"><strong>Introduction</strong></span><br />
This tutorial will guide you through creating MySQLi prepared statements.  <em>MySQLi </em>is an extension/API for PHP that is also know as MySQL <strong>Improved</strong>. MySQLi is included with versions 5 of PHP and later that allows PHP developers to take advantage of all the features in MySQL 4.1.3. According to the manual:</p>
<p><span id="more-5"></span></p>
<div style="margin:5px 20px 20px;">
<div style="margin-bottom:2px;">Quote:</div>
<table border="0" cellspacing="0" cellpadding="6" width="100%">
<tbody>
<tr>
<td style="border:1px inset;">If you are using MySQL versions 4.1.3 or later it is       <em>strongly</em> recommended that you use the       <em>mysqli</em> extension instead.</td>
</tr>
</tbody>
</table>
</div>
<p>Any more introduction to MySQLi is beyond the scope of this tutorial. You can read more at <a href="http://www.php.net/manual/en/intro.mysqli.php" target="_blank">PHP: Introduction &#8211; Manual</a></p>
<p><span style="font-size:medium;"><strong>Prepared Statements</strong></span><br />
Perpared statements are queries written before any actual data is passed to the query. You setup a query once and have the ability to execute it multiple times, binding different sets of data while using only one query.</p>
<p><a href="http://dev.mysql.com/tech-resources/articles/4.1/prepared-statements.html" target="_blank">MySQL</a> Official definition:</p>
<div style="margin:5px 20px 20px;">
<div style="margin-bottom:2px;">Quote:</div>
<table border="0" cellspacing="0" cellpadding="6" width="100%">
<tbody>
<tr>
<td style="border:1px inset;">Prepared statements are the ability to set up a statement once, and then execute it many times with different parameters</td>
</tr>
</tbody>
</table>
</div>
<p>You may be wondering why you would use prepared statements as opposed to passing the SQL statement directoy to MySQL. There are three main benefits to using prepared statements:</p>
<ol style="list-style-type:decimal;">
<li>Significant performance benefit if you are running the same query multiple times. Creating a normal query (non-prepared) has the additional overhead of parsing the statement for syntax errors and setup for the query to be ran. When using prepared statements in MySQL this overhead is only preformed once (the first time) thus increasing each subsequent use.</li>
<li>Passing variables as parameters is more secure than passing unvalidated data into a SQL query. Prepared statements make it harder to perform SQL Injection by seperating SQL logic from from the data.</li>
<li>Binding variables is cleaner and more convenient for the developer.</li>
</ol>
<p><span style="font-size:medium;"><strong>Types</strong></span><br />
There are two types of prepared statements: bound parameter and bound result. As you can guess, bound parameter prepared statements take an input (insert, update) SQL statement and allows the developer to create a template for SQL execution. Bound result prepared statements allow the developer to extract data from a bound SQL query.</p>
<p><span style="font-size:medium;"><strong>SQL Code</strong></span><br />
To create a template in a prepared statement replace all values with question marks (?). Lets examine a non-prepared insert query:</p>
<p><strong>Bound Parameters</strong></p>
<div style="margin:5px 20px 20px;">
<div style="margin-bottom:2px;">Code:</div>
<pre style="border:1px inset;overflow:auto;width:550px;height:34px;text-align:left;font-family:Courier New,monospace;margin:0;padding:6px;">INSERT INTO CodeCall (FirstName, LastName) VALUES ('Jordan','DeLozier');</pre>
</div>
<p>Changing this to a bound parameter prepared statement means replacing the values with ?:</p>
<div style="margin:5px 20px 20px;">
<div style="margin-bottom:2px;">Code:</div>
<pre style="border:1px inset;overflow:auto;width:550px;height:34px;text-align:left;font-family:Courier New,monospace;margin:0;padding:6px;">INSERT INTO CodeCall (FirstName, LastName) VALUES (?, ?);</pre>
</div>
<div style="margin:5px 20px 20px;">
<div style="margin-bottom:2px;">Code:</div>
<pre style="border:1px inset;overflow:auto;width:550px;height:34px;text-align:left;font-family:Courier New,monospace;margin:0;padding:6px;">SELECT FirstName,LastName FROM CodeCall WHERE FirstName='Jordan';</pre>
</div>
<p>Will be converted into:</p>
<div style="margin:5px 20px 20px;">
<div style="margin-bottom:2px;">Code:</div>
<pre style="border:1px inset;overflow:auto;width:550px;height:34px;text-align:left;font-family:Courier New,monospace;margin:0;padding:6px;">SELECT FirstName,LastName FROM CodeCall WHERE FirstName=?;</pre>
</div>
<p><strong>Bound Results</strong><br />
There is no SQL conversion for bound results. Rather, bound results assign the results to variables similar to list() language construct or extract().</p>
<p><span style="font-size:medium;"><strong>PHP Code</strong></span><br />
Natuarlly, you&#8217;ll need an active MySQLi connection. You can find my database and table structure in the attached SQL file. I&#8217;ll be using user root with no password, you may need to change.</p>
<div style="margin:5px 20px 20px;">
<div style="margin-bottom:2px;">PHP Code:</div>
<div style="border:1px inset;overflow:auto;width:550px;height:146px;text-align:left;margin:0;padding:6px;" dir="ltr"><code style="white-space:nowrap;"> <!-- php buffer start --><code><span style="color:#000000;"> <span style="color:#0000bb;">&lt;?php<br />
$mysqli </span><span style="color:#007700;">= new </span><span style="color:#0000bb;">mysqli</span><span style="color:#007700;">(</span><span style="color:#dd0000;">"localhost"</span><span style="color:#007700;">, </span><span style="color:#dd0000;">"root"</span><span style="color:#007700;">, </span><span style="color:#dd0000;">""</span><span style="color:#007700;">, </span><span style="color:#dd0000;">"cctutorial_mysqli"</span><span style="color:#007700;">);</p>
<p></span><span style="color:#ff8000;">/* check connection */<br />
</span><span style="color:#007700;">if (</span><span style="color:#0000bb;">mysqli_connect_errno</span><span style="color:#007700;">()) {<br />
</span><span style="color:#0000bb;">printf</span><span style="color:#007700;">(</span><span style="color:#dd0000;">"Connect failed: %s\n"</span><span style="color:#007700;">, </span><span style="color:#0000bb;">mysqli_connect_error</span><span style="color:#007700;">());<br />
exit();<br />
}</span> </span> </code><!-- php buffer end --> </code></div>
</div>
<p>No link needs to be passed to mysqli_connect_error because at the time of connection, if there is an error, link is null. mysqli_connect_error simply grabs the last connection error and returns blank if none. Output is similar to this:</p>
<div style="margin:5px 20px 20px;">
<div style="margin-bottom:2px;">Quote:</div>
<table border="0" cellspacing="0" cellpadding="6" width="100%">
<tbody>
<tr>
<td style="border:1px inset;">Warning: mysqli::mysqli() [mysqli.mysqli]: (42000/1044): Access denied for user &#8221;@&#8217;localhost&#8217; to database &#8216;cctutorial_mysqli&#8217; in C:\wamp\www\PHP_Test\mysqli_prepared.php on line 3<br />
Connect failed: Access denied for user &#8221;@&#8217;localhost&#8217; to database &#8216;cctutorial_mysqli&#8217;</td>
</tr>
</tbody>
</table>
</div>
<p>*Note: You can also bind parameters to SELECT statements.</p>
<p><strong>Bound Parameters</strong><br />
Now that we have that out of the way we will want to create our prepared statement:</p>
<div style="margin:5px 20px 20px;">
<div style="margin-bottom:2px;">PHP Code:</div>
<div style="border:1px inset;overflow:auto;width:550px;height:498px;text-align:left;margin:0;padding:6px;" dir="ltr"><code style="white-space:nowrap;"> <!-- php buffer start --><code><span style="color:#000000;"> <span style="color:#ff8000;">/* Create the prepared statement */<br />
</span><span style="color:#007700;">if (</span><span style="color:#0000bb;">$stmt </span><span style="color:#007700;">= </span><span style="color:#0000bb;">$mysqli</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">prepare</span><span style="color:#007700;">(</span><span style="color:#dd0000;">"INSERT INTO CodeCall (FirstName, LastName) values (?, ?)"</span><span style="color:#007700;">)) {</p>
<p></span><span style="color:#ff8000;">/* Bind our params */<br />
</span><span style="color:#0000bb;">$stmt</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">bind_param</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'ss'</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$firstName</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$lastName</span><span style="color:#007700;">);</p>
<p></span><span style="color:#ff8000;">/* Set our params */<br />
</span><span style="color:#0000bb;">$firstName </span><span style="color:#007700;">= </span><span style="color:#dd0000;">"Jordan"</span><span style="color:#007700;">;<br />
</span><span style="color:#0000bb;">$lastName </span><span style="color:#007700;">= </span><span style="color:#dd0000;">"DeLozier"</span><span style="color:#007700;">;</p>
<p></span><span style="color:#ff8000;">/* Execute the prepared Statement */<br />
</span><span style="color:#0000bb;">$stmt</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">execute</span><span style="color:#007700;">();</p>
<p></span><span style="color:#ff8000;">/* Echo results */<br />
</span><span style="color:#007700;">echo </span><span style="color:#dd0000;">"Inserted {$lastName},{$firstName} into database\n"</span><span style="color:#007700;">;</p>
<p></span><span style="color:#ff8000;">/* Set our params for second query */<br />
</span><span style="color:#0000bb;">$firstName </span><span style="color:#007700;">= </span><span style="color:#dd0000;">"John"</span><span style="color:#007700;">;<br />
</span><span style="color:#0000bb;">$lastName </span><span style="color:#007700;">= </span><span style="color:#dd0000;">"Ciacia"</span><span style="color:#007700;">;</p>
<p></span><span style="color:#ff8000;">/* Execute second Query */<br />
</span><span style="color:#0000bb;">$stmt</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">execute</span><span style="color:#007700;">();</p>
<p>echo</p>
<p></span><span style="color:#dd0000;">"Inserted {$lastName},{$firstName} into database\n"</span><span style="color:#007700;">;</p>
<p></span><span style="color:#ff8000;">/* Close the statement */<br />
</span><span style="color:#0000bb;">$stmt</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">close</span><span style="color:#007700;">();<br />
}<br />
else {<br />
</span><span style="color:#ff8000;">/* Error */<br />
</span><span style="color:#0000bb;">printf</span><span style="color:#007700;">(</span><span style="color:#dd0000;">"Prepared Statement Error: %s\n"</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$mysqli</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">error</span><span style="color:#007700;">);<br />
}<br />
</span> </span> </code><!-- php buffer end --> </code></div>
</div>
<p>The above script creates a prepared statement:</p>
<div style="margin:5px 20px 20px;">
<div style="margin-bottom:2px;">PHP Code:</div>
<div style="border:1px inset;overflow:auto;width:550px;height:34px;text-align:left;margin:0;padding:6px;" dir="ltr"><code style="white-space:nowrap;"> <!-- php buffer start --><code><span style="color:#000000;"> <span style="color:#0000bb;">$stmt </span><span style="color:#007700;">= </span><span style="color:#0000bb;">$mysqli</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">prepare</span><span style="color:#007700;">(</span><span style="color:#dd0000;">"INSERT INTO CodeCall (FirstName, LastName) values (?, ?)"</span><span style="color:#007700;">)<br />
</span> </span> </code><!-- php buffer end --> </code></div>
</div>
<p>If there is any error with your SQL statement an error is thrown and displayed to the user:</p>
<div style="margin:5px 20px 20px;">
<div style="margin-bottom:2px;">PHP Code:</div>
<div style="border:1px inset;overflow:auto;width:550px;height:34px;text-align:left;margin:0;padding:6px;" dir="ltr"><code style="white-space:nowrap;"> <!-- php buffer start --><code><span style="color:#000000;"> <span style="color:#0000bb;">printf</span><span style="color:#007700;">(</span><span style="color:#dd0000;">"Prepared Statement Error: %s\n"</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$mysqli</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">error</span><span style="color:#007700;">);<br />
</span> </span> </code><!-- php buffer end --> </code></div>
</div>
<p>You may want to remove this in production.  Next we bind two variables to the statement object, $stmt:</p>
<div style="margin:5px 20px 20px;">
<div style="margin-bottom:2px;">PHP Code:</div>
<div style="border:1px inset;overflow:auto;width:550px;height:34px;text-align:left;margin:0;padding:6px;" dir="ltr"><code style="white-space:nowrap;"> <!-- php buffer start --><code><span style="color:#000000;"> <span style="color:#0000bb;">$stmt</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">bind_param</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'ss'</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$firstName</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$lastName</span><span style="color:#007700;">);<br />
</span> </span> </code><!-- php buffer end --> </code></div>
</div>
<p>Notice we pass three items to the function but we only have two places for variables in our prepared statement. This is because the first argument of bind_param is specifying the bind the types for the corresponding bind values. The values can be:</p>
<p><strong>i</strong> &#8211; Integer<br />
<strong>d</strong> &#8211; Decimal<br />
<strong>s</strong> &#8211; String<br />
<strong>b</strong> &#8211; Blob (sent in packets)</p>
<p>If you have 5 variables and they are all strings you specify five types (&#8220;sssss&#8221;) as the first param. If you have 3 strings, 1 integer and 1 decimal you specify the types as such: &#8220;sssid&#8221;. Of course, they must be in the correct order. If the integer is first and the decimal is third it would look like this: &#8220;isdss&#8221;.</p>
<p>Next we assign the variables values.  Notice that the variables had no value even though we bound them.</p>
<div style="margin:5px 20px 20px;">
<div style="margin-bottom:2px;">PHP Code:</div>
<div style="border:1px inset;overflow:auto;width:550px;height:50px;text-align:left;margin:0;padding:6px;" dir="ltr"><code style="white-space:nowrap;"> <!-- php buffer start --><code><span style="color:#000000;"> <span style="color:#0000bb;">$firstName </span><span style="color:#007700;">= </span><span style="color:#dd0000;">"Jordan"</span><span style="color:#007700;">;<br />
</span><span style="color:#0000bb;">$lastName </span><span style="color:#007700;">= </span><span style="color:#dd0000;">"DeLozier"</span><span style="color:#007700;">;<br />
</span> </span> </code><!-- php buffer end --> </code></div>
</div>
<p>The final step is to execute the query:</p>
<div style="margin:5px 20px 20px;">
<div style="margin-bottom:2px;">PHP Code:</div>
<div style="border:1px inset;overflow:auto;width:550px;height:34px;text-align:left;margin:0;padding:6px;" dir="ltr"><code style="white-space:nowrap;"> <!-- php buffer start --><code><span style="color:#000000;"> <span style="color:#0000bb;">$stmt</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">execute</span><span style="color:#007700;">();<br />
</span> </span> </code><!-- php buffer end --> </code></div>
</div>
<p>The execute takes the prepared statement, replaces the question marks (?) with our bound parameter values ($firstName and $lastName) and executes the query. In order to show the convenience of executing the same SQL statement multiple times we also insert John Ciacia into the database:</p>
<div style="margin:5px 20px 20px;">
<div style="margin-bottom:2px;">PHP Code:</div>
<div style="border:1px inset;overflow:auto;width:550px;height:114px;text-align:left;margin:0;padding:6px;" dir="ltr"><code style="white-space:nowrap;"> <!-- php buffer start --><code><span style="color:#000000;"> <span style="color:#ff8000;">/* Set our params for second query */<br />
</span><span style="color:#0000bb;">$firstName </span><span style="color:#007700;">= </span><span style="color:#dd0000;">"John"</span><span style="color:#007700;">;<br />
</span><span style="color:#0000bb;">$lastName </span><span style="color:#007700;">= </span><span style="color:#dd0000;">"Ciacia"</span><span style="color:#007700;">;</p>
<p></span><span style="color:#ff8000;">/* Execute second Query */<br />
</span><span style="color:#0000bb;">$stmt</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">execute</span><span style="color:#007700;">();<br />
</span> </span> </code><!-- php buffer end --> </code></div>
</div>
<p>Notice how easy it was to change the variable values and execute the statement again? If you are following along and run the script at this point you will see this output:</p>
<div style="margin:5px 20px 20px;">
<div style="margin-bottom:2px;">Quote:</div>
<table border="0" cellspacing="0" cellpadding="6" width="100%">
<tbody>
<tr>
<td style="border:1px inset;">Inserted DeLozier,Jordan into database<br />
Inserted Ciacia,John into database</td>
</tr>
</tbody>
</table>
</div>
<p>If anything goes wrong, you may see an error such as this:</p>
<div style="margin:5px 20px 20px;">
<div style="margin-bottom:2px;">Quote:</div>
<table border="0" cellspacing="0" cellpadding="6" width="100%">
<tbody>
<tr>
<td style="border:1px inset;">Prepared Statement Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near &#8216;INSERTs INTO CodeCall (FirstName, LastName) values (?, ?)&#8217; at line 1</td>
</tr>
</tbody>
</table>
</div>
<p>I simply added an &#8220;s&#8221; to INSERT to make the statement invalid.</p>
<p><strong>Bound Results</strong><br />
That covers input and most of the information there will apply to output so I will make this section short. Using the same script above we will add another prepared statement and select the data that we just inserted.</p>
<div style="margin:5px 20px 20px;">
<div style="margin-bottom:2px;">PHP Code:</div>
<div style="border:1px inset;overflow:auto;width:550px;height:354px;text-align:left;margin:0;padding:6px;" dir="ltr"><code style="white-space:nowrap;"> <!-- php buffer start --><code><span style="color:#000000;"> <span style="color:#ff8000;">/* Create the prepared statement */<br />
</span><span style="color:#007700;">if (</span><span style="color:#0000bb;">$stmt </span><span style="color:#007700;">= </span><span style="color:#0000bb;">$mysqli</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">prepare</span><span style="color:#007700;">(</span><span style="color:#dd0000;">"SELECT FirstName,LastName FROM CodeCall ORDER BY LastName"</span><span style="color:#007700;">)) {<br />
</span><span style="color:#ff8000;">/* Execute the prepared Statement */<br />
</span><span style="color:#0000bb;">$stmt</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">execute</span><span style="color:#007700;">();</p>
<p></span><span style="color:#ff8000;">/* Bind results to variables */<br />
</span><span style="color:#0000bb;">$stmt</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">bind_result</span><span style="color:#007700;">(</span><span style="color:#0000bb;">$firstName</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$lastName</span><span style="color:#007700;">);</p>
<p></span><span style="color:#ff8000;">/* fetch values */<br />
</span><span style="color:#007700;">while (</span><span style="color:#0000bb;">$stmt</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">fetch</span><span style="color:#007700;">()) {<br />
</span><span style="color:#0000bb;">printf</span><span style="color:#007700;">(</span><span style="color:#dd0000;">"%s %s\n"</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$lastName</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$firstName</span><span style="color:#007700;">);<br />
}</p>
<p></span><span style="color:#ff8000;">/* Close the statement */<br />
</span><span style="color:#0000bb;">$stmt</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">close</span><span style="color:#007700;">();</p>
<p>}<br />
else {</p>
<p></span><span style="color:#ff8000;">/* Error */<br />
</span><span style="color:#0000bb;">printf</span><span style="color:#007700;">(</span><span style="color:#dd0000;">"Prepared Statement Error: %s\n"</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$mysqli</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">error</span><span style="color:#007700;">);<br />
}<br />
</span> </span> </code><!-- php buffer end --> </code></div>
</div>
<p>Everything looks familar here. You use bind_results instead of bind_params and the variables are assigned a value for you. Use fetch() to itterate through the results and print a value.</p>
<p>You should see the following output:</p>
<div style="margin:5px 20px 20px;">
<div style="margin-bottom:2px;">Quote:</div>
<table border="0" cellspacing="0" cellpadding="6" width="100%">
<tbody>
<tr>
<td style="border:1px inset;">Ciacia John<br />
DeLozier Jordan</td>
</tr>
</tbody>
</table>
</div>
<p><span style="font-size:medium;"><strong>Conclusion</strong></span><br />
MySQLi offers many benefits over traditional mysql and allows you to use all of the features of MySQL 4.1.3. You should use this extension if you are developing in PHP version 5 or above and using MySQL 4.1.3+. Using prepared statements will allow you to save resources (CPU, Memory, etc) which could be vital in many circumstances (shared hosting comes to mind) and reduce the thread of SQL Injection.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/programmingcorner.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/programmingcorner.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/programmingcorner.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/programmingcorner.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/programmingcorner.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/programmingcorner.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/programmingcorner.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/programmingcorner.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/programmingcorner.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/programmingcorner.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/programmingcorner.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/programmingcorner.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/programmingcorner.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/programmingcorner.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingcorner.wordpress.com&amp;blog=7638624&amp;post=5&amp;subd=programmingcorner&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://programmingcorner.wordpress.com/2009/07/21/mysqli-prepared-statements/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5c98c594d03592c54a02668ebead43ee?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mucchuaonline</media:title>
		</media:content>
	</item>
		<item>
		<title>Adding a row in mySQL database using web interface</title>
		<link>http://programmingcorner.wordpress.com/2009/07/20/adding-a-row-in-mysql-database-using-web-interface/</link>
		<comments>http://programmingcorner.wordpress.com/2009/07/20/adding-a-row-in-mysql-database-using-web-interface/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 03:12:07 +0000</pubDate>
		<dc:creator>mucchuaonline</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://programmingcorner.wordpress.com/?p=3</guid>
		<description><![CDATA[source: http://www.tutorialplanet.net/2009/05/29/adding-a-row-in-mysql-database/<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingcorner.wordpress.com&amp;blog=7638624&amp;post=3&amp;subd=programmingcorner&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Alright, adding data into database is pretty simple. Basically, the whole thing can be divided into two parts. A web form and php code that corresponds to it.</p>
<p>Before we get started we create a table in our database which will store the values we are going to submit through our form. We have a database called medical_company, with a table called employees. The database can be created using phpMyAdmin or any other mySQL gui program such as mysqlyog.</p>
<p><span id="more-3"></span></p>
<p>Here is the query for creating the table:</p>
<blockquote><p>CREATE TABLE `employees` ( `id` int(11) NOT NULL auto_increment, `name` text NOT NULL, `email` longtext NOT NULL, PRIMARY KEY (`id`) ) TYPE=MyISAM AUTO_INCREMENT=1 ;</p></blockquote>
<p>Next we need to create an html form which will be used to enter the data. Copy the code below and paste it in a new file called <em><strong>insert.php</strong></em></p>
<blockquote><p>&lt;form action=&#8221;" method=&#8221;post&#8221;&gt;<br />
&lt;p&gt;Enter the new employee:&lt;r /&gt;<br />
FirstName: &lt;input type=&#8221;text&#8221; name=&#8221;name&#8221; size=&#8221;20&#8243; maxlength=&#8221;255&#8243;<br />
/&gt;&lt;br /&gt;<br />
Email: &lt;input type=&#8221;text&#8221; name=&#8221;email&#8221; size=&#8221;20&#8243; maxlength=&#8221;255&#8243;<br />
/&gt;&lt;br /&gt;<br />
&lt;input type=&#8221;submit&#8221; name=&#8221;submit&#8221; value=&#8221;SUBMIT&#8221; /&gt;&lt;/p&gt;<br />
&lt;/form&gt;</p></blockquote>
<p>Since we are using a single page for our html form and php code, we will keep <em>action </em>attribute of our form to empty as you can see in the above code <em><strong>action=&#8221;"</strong></em>.</p>
<p>Now what we need from this form is that when the submit button is pressed it process the php code, connect to the database and insert name and e-mail values to our data table. Here is the php code which will be executed once the submit is hit:</p>
<blockquote><p>&lt;?php//check if submit is pressed</p>
<p>if (isset($_POST['submit']))<br />
{</p>
<p>// connecting to database</p>
<p>$connection = mysql_connect(’localhost’, ‘username’, ‘password’);<br />
mysql_select_db(’medicalcompany’);</p>
<p>//inserting values</p>
<p>$name = $_POST['name'];<br />
$email = $_POST['email'];<br />
$result=MYSQL_QUERY(&#8220;INSERT INTO employees (id,name,email)&#8221;.<br />
&#8220;VALUES (’NULL’, ‘$name’, ‘$email’)&#8221;);</p>
<p>//confirm</p>
<p>if ($result) {<br />
echo(’&lt;p&gt;New employee added&lt;/p&gt;’);<br />
} else {<br />
echo(’&lt;p&gt;Error adding new employee: ‘ . mysql_error() . ‘&lt;/p&gt;’);<br />
}<br />
}<br />
?&gt;</p></blockquote>
<p>Now that is basically all the php code we need for processing form. This code is executed only if the submit button is pressed. The<em><strong> if (isset($_POST['submit'])) </strong></em>commands checks if the submit button is pressed. Please note that the name of button corresponds the name inside the command. So, be sure to name your submit button same as what is written inside <em><strong>$_POST['']. </strong></em>It is case sensitive too.</p>
<p>Next, we create a <em><strong>$connection </strong></em>variable to connect to our database. We use a php command mysql_connect() which contains information about our database, which includes server name (in this example we use <strong><em>localhost</em></strong> as servername), a username and a password for the database.</p>
<p>Then we use a php command <em><strong>mysql_select_db() </strong></em>to select our database. We are using a database called ‘medicalcompany’ in this example.</p>
<p>Then we create a two variables $name and $email  which will contain the values of our textfield in the html form we created. Now we execute an insert query using a php command <em><strong>mysql_query(). </strong></em>I hope you know the basics of sql queries. Basically what it does is that it inserts our data into the table called ‘employees’<strong> </strong>which contains the information about employees<strong>.</strong></p>
<p><strong></strong>In the end we used the ‘if statement’ to check if the query has been executed successfully or not.<br />
<strong><br />
</strong>In next tutorial we will discuss how to retrieve the date from mysql darabase.<strong><br />
</strong></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/programmingcorner.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/programmingcorner.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/programmingcorner.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/programmingcorner.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/programmingcorner.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/programmingcorner.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/programmingcorner.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/programmingcorner.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/programmingcorner.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/programmingcorner.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/programmingcorner.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/programmingcorner.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/programmingcorner.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/programmingcorner.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=programmingcorner.wordpress.com&amp;blog=7638624&amp;post=3&amp;subd=programmingcorner&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://programmingcorner.wordpress.com/2009/07/20/adding-a-row-in-mysql-database-using-web-interface/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5c98c594d03592c54a02668ebead43ee?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mucchuaonline</media:title>
		</media:content>
	</item>
	</channel>
</rss>
