<?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>Theme Heven &#187; Php</title>
	<atom:link href="http://www.themeheven.com/category/tutorial/php-tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.themeheven.com</link>
	<description>WordPress, Ajax, Jquery, Web 2.0, PHP, Tutorials, Tips, Tricks</description>
	<lastBuildDate>Wed, 13 Jan 2010 09:37:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>10 Simple PHP Login Sytem Tutorial</title>
		<link>http://www.themeheven.com/2010/01/10-simple-php-login-sytem-tutorial/</link>
		<comments>http://www.themeheven.com/2010/01/10-simple-php-login-sytem-tutorial/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 09:29:47 +0000</pubDate>
		<dc:creator>themeheven</dc:creator>
				<category><![CDATA[HOW TO]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.themeheven.com/?p=279</guid>
		<description><![CDATA[Login System is the First Part of Security system in web programming. In this tutorial I am going to show you the collection of various methos and types of login system. I will give a right choice for your system and you can understand more about login.
More here
See Also Following Topics:

An Introduction to Object Oriented [...]]]></description>
			<content:encoded><![CDATA[<p>Login System is the First Part of Security system in web programming. In this tutorial I am going to show you the collection of various methos and types of login system. I will give a right choice for your system and you can understand more about login.</p>
<p><a href="http://www.jagadishwor.com.np/2009/12/10-simple-php-login-sytem-tutorial/">More here</a><br />
<h3>See Also Following Topics:</h3>
<ul class="related_post">
<li><a href="http://www.themeheven.com/2010/01/an-introduction-to-object-oriented-php/" title="An Introduction to Object Oriented PHP">An Introduction to Object Oriented PHP</a></li>
<li><a href="http://www.themeheven.com/2009/10/php-image-file-upload-tutorial/" title="PHP Image File Upload Tutorial">PHP Image File Upload Tutorial</a></li>
<li><a href="http://www.themeheven.com/2009/08/php-redirect-to-another-pageurl-script/" title="PHP Redirect to another Page/URL Script">PHP Redirect to another Page/URL Script</a></li>
<li><a href="http://www.themeheven.com/2009/08/delete-multiple-records-using-checkbox-with-select-all-buttons/" title="Delete Multiple Records Using Checkbox With Select All Buttons">Delete Multiple Records Using Checkbox With Select All Buttons</a></li>
<li><a href="http://www.themeheven.com/2009/06/advanced-php-query-function/" title="Advanced PHP Query function">Advanced PHP Query function</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.themeheven.com/2010/01/10-simple-php-login-sytem-tutorial/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP Image File Upload Tutorial</title>
		<link>http://www.themeheven.com/2009/10/php-image-file-upload-tutorial/</link>
		<comments>http://www.themeheven.com/2009/10/php-image-file-upload-tutorial/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 08:41:53 +0000</pubDate>
		<dc:creator>themeheven</dc:creator>
				<category><![CDATA[HOW TO]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.themeheven.com/?p=268</guid>
		<description><![CDATA[In this tutorial you will learn to upload image file using PHP. Together you will learn to restrict file type. In this tutorial I am showing you example in which you can upload only jpg/jpeg images. It a very easy method I am going to show you and hope you will understand.
Start With Form :
&#60;form [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial you will learn to upload image file using PHP. Together you will learn to restrict file type. In this tutorial I am showing you example in which you can upload only jpg/jpeg images. It a very easy method I am going to show you and hope you will understand.</p>
<p>Start With Form :</p>
<p><code>&lt;form action="" method="post" enctype="multipart/form-data" name="form1"&gt;&lt;p&gt;<br />
&lt;input name="fileImage" type="file" id="fileImage"&gt;<br />
&lt;/p&gt;&lt;p&gt;<br />
&lt;input name="save-image" type="submit" id="save-image" value="Upload Image"&gt;<br />
&lt;/p&gt;</p>
<p>&lt;/form&gt;</p>
<p></code></p>
<p>Upload Code:</p>
<p><code>&lt;?phpif(isset($_POST['save-image'])){//Line Use to check if the buttton was pressed.</p>
<p>$fileName=$_FILES['fileImage']['name'];</p>
<p>//Getting file name and assigning it to $fileName variable</p>
<p>$fileType=$_FILES['fileImage']['type'];</p>
<p>//Getting file type and assigning it to $filetype variable</p>
<p>$fileSize=$_FILES['fileImage']['size'];</p>
<p>//Getting file size and assigning it to $filesize;</p>
<p>$tmpName=$_FILES['fileImage']['tmp_name'];</p>
<p>//Getting temporary name of file and assigning it to $tmpName</p>
<p>$maxsize=1048576;</p>
<p>//Defining maximum file size to 1Mb</p>
<p>$uploadDir="upload/";</p>
<p>//Define upload directory</p>
<p>$filePath=$uploadDir.$fileName;</p>
<p>//Defining file path so that we can use it on move_uploaded_file function</p>
<p>if(($fileType=="image/jpg") || ($fileType=="image/jpeg") || ($fileType=="image/pjpeg")){</p>
<p>//Checking file type. Is the file jpg or not?</p>
<p>$result=move_uploaded_file($tmpName, $filePath);</p>
<p>//When file is jpg moving file to the upload directory from temp directory</p>
<p>}</p>
<p>if($result){</p>
<p>//If file move successfully then show message</p>
<p>echo "File Uploaded Successfully";</p>
<p>}else{</p>
<p>//If file is invalid display message</p>
<p>echo "Invalid File Format";</p>
<p>}</p>
<p>}</p>
<p>?&gt;</p>
<p></code></p>
<p>Full Code:</p>
<p><code>&lt;?phpif(isset($_POST['save-image'])){$fileName=$_FILES['fileImage']['name'];</p>
<p>$fileType=$_FILES['fileImage']['type'];</p>
<p>$fileSize=$_FILES['fileImage']['size'];</p>
<p>$tmpName=$_FILES['fileImage']['tmp_name'];</p>
<p>$maxsize=1048576;</p>
<p>$uploadDir="upload/";</p>
<p>$filePath=$uploadDir.$fileName;</p>
<p>if(($fileType=="image/jpg") || ($fileType=="image/jpeg") || ($fileType=="image/pjpeg")){</p>
<p>$result=move_uploaded_file($tmpName, $filePath);</p>
<p>}</p>
<p>if($result){</p>
<p>echo "File Uploaded Successfully";</p>
<p>}else{</p>
<p>echo "Invalid File Format";</p>
<p>}</p>
<p>}</p>
<p>?&gt;</p>
<p>&lt;html&gt;</p>
<p>&lt;head&gt;</p>
<p>&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;</p>
<p>&lt;title&gt;Upload Image&lt;/title&gt;</p>
<p>&lt;/head&gt;</p>
<p>&lt;body&gt;</p>
<p>&lt;form action="" method="post" enctype="multipart/form-data" name="form1"&gt;</p>
<p>&lt;p&gt;</p>
<p>&lt;input name="fileImage" type="file" id="fileImage"&gt;</p>
<p>&lt;/p&gt;</p>
<p>&lt;p&gt;</p>
<p>&lt;input name="save-image" type="submit" id="save-image" value="Upload Image"&gt;</p>
<p>&lt;/p&gt;</p>
<p>&lt;/form&gt;</p>
<p>&lt;/body&gt;</p>
<p>&lt;/html&gt;</p>
<p></code><br />
<h3>See Also Following Topics:</h3>
<ul class="related_post">
<li><a href="http://www.themeheven.com/2010/01/10-simple-php-login-sytem-tutorial/" title="10 Simple PHP Login Sytem Tutorial ">10 Simple PHP Login Sytem Tutorial </a></li>
<li><a href="http://www.themeheven.com/2010/01/an-introduction-to-object-oriented-php/" title="An Introduction to Object Oriented PHP">An Introduction to Object Oriented PHP</a></li>
<li><a href="http://www.themeheven.com/2009/08/php-redirect-to-another-pageurl-script/" title="PHP Redirect to another Page/URL Script">PHP Redirect to another Page/URL Script</a></li>
<li><a href="http://www.themeheven.com/2009/08/delete-multiple-records-using-checkbox-with-select-all-buttons/" title="Delete Multiple Records Using Checkbox With Select All Buttons">Delete Multiple Records Using Checkbox With Select All Buttons</a></li>
<li><a href="http://www.themeheven.com/2009/06/advanced-php-query-function/" title="Advanced PHP Query function">Advanced PHP Query function</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.themeheven.com/2009/10/php-image-file-upload-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Redirect to another Page/URL Script</title>
		<link>http://www.themeheven.com/2009/08/php-redirect-to-another-pageurl-script/</link>
		<comments>http://www.themeheven.com/2009/08/php-redirect-to-another-pageurl-script/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 08:21:19 +0000</pubDate>
		<dc:creator>themeheven</dc:creator>
				<category><![CDATA[HOW TO]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[redirect]]></category>

		<guid isPermaLink="false">http://www.themeheven.com/?p=253</guid>
		<description><![CDATA[In  PHP there are some common way to redirect another page or URL. Here I am going to use some simple way to redirect. In PHP you need to use header() to send a raw HTTP header. In headers() method, you can easily redirect to the new page or URL.
Using headers():
In  header() method [...]]]></description>
			<content:encoded><![CDATA[<p>In  PHP there are some common way to redirect another page or URL. Here I am going to use some simple way to redirect. In PHP you need to use header() to send a raw HTTP header. In headers() method, you can easily redirect to the new page or URL.</p>
<h2>Using headers():</h2>
<p>In  header() method you must remember to call it before any output is sent. Means nothing should be printed in page.</p>
<pre>&lt;?php

  //Redirect to page or URL 

  header(&quot;Location: URL or Page Address &quot;);

  //End the code with exit 

  exit;

  ?&gt; </pre>
<h2>Using Javascritp:</h2>
<p>In this method you can use this JavaScript at any part of your page. </p>
<p>
<pre>&lt;script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;&gt;

window.location = &quot;redirect page/URL here&quot;

&lt;/script&gt; </pre>
</p>
<p>You can also show any error message to your user:</p>
<p>
<pre>&lt;script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;&gt;alert(&quot;any message here &quot;)

window.location = &quot;redirect page/URL here&quot;

&lt;/script&gt; </pre>
</p>
<h2>Using Meta tag method:<br />
</h2>
<p>You can use this script at any part of your page. </p>
<p>
<pre>&lt;?php

  echo &quot;&lt;meta http-equiv='refresh' content='0;url=redirect page/URL here'&gt;&quot;;

?&gt;
</pre>
<p>Enjoy!!!</p>
<h3>See Also Following Topics:</h3>
<ul class="related_post">
<li><a href="http://www.themeheven.com/2010/01/10-simple-php-login-sytem-tutorial/" title="10 Simple PHP Login Sytem Tutorial ">10 Simple PHP Login Sytem Tutorial </a></li>
<li><a href="http://www.themeheven.com/2010/01/an-introduction-to-object-oriented-php/" title="An Introduction to Object Oriented PHP">An Introduction to Object Oriented PHP</a></li>
<li><a href="http://www.themeheven.com/2009/10/php-image-file-upload-tutorial/" title="PHP Image File Upload Tutorial">PHP Image File Upload Tutorial</a></li>
<li><a href="http://www.themeheven.com/2009/08/delete-multiple-records-using-checkbox-with-select-all-buttons/" title="Delete Multiple Records Using Checkbox With Select All Buttons">Delete Multiple Records Using Checkbox With Select All Buttons</a></li>
<li><a href="http://www.themeheven.com/2009/06/advanced-php-query-function/" title="Advanced PHP Query function">Advanced PHP Query function</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.themeheven.com/2009/08/php-redirect-to-another-pageurl-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Delete Multiple Records Using Checkbox With Select All Buttons</title>
		<link>http://www.themeheven.com/2009/08/delete-multiple-records-using-checkbox-with-select-all-buttons/</link>
		<comments>http://www.themeheven.com/2009/08/delete-multiple-records-using-checkbox-with-select-all-buttons/#comments</comments>
		<pubDate>Sun, 09 Aug 2009 17:16:52 +0000</pubDate>
		<dc:creator>themeheven</dc:creator>
				<category><![CDATA[HOW TO]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[Mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.themeheven.com/?p=232</guid>
		<description><![CDATA[
Last Time I post &#8220;Deleting multiple records using checkbox&#8221;. One of my friend request me to add some features like yahoo mail, gmail where we can select all or unselect all records. WOW! it was really helpful for everyone. So I am here going to show you Delete Multiple Records Using Select All and Unselect [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.jagadishwor.com.np/wp-content/uploads/2009/08/seldelmult.gif" alt="" width="376" height="185" /></p>
<p>Last Time I post &#8220;Deleting multiple records using checkbox&#8221;. One of my friend request me to add some features like yahoo mail, gmail where we can select all or unselect all records. WOW! it was really helpful for everyone. So I am here going to show you Delete Multiple Records Using Select All and Unselect Features. I have simply added a javascript code in my previous post which you can <a href="http://www.jagadishwor.com.np/2009/08/php-mysql-deleting-multiple-records-using-checkbox/" target="_blank">see here</a>. All of the Features are same as my previous code and I am using the new codes which I added on my last one.</p>
<p>Now Create your database and table as your required. Write your connection string and finish all of the work. Which you can see in my previous post.</p>
<p>Ok Now add this javascript before your &lt;/head&gt; tag.</p>
<pre><code>&lt;script&gt;
function selectAllCheckBoxes(FormName, FieldName, CheckValue)
{
	if(!document.forms[FormName])
		return;
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if(!objCheckBoxes)
		return;
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes)
		objCheckBoxes.checked = CheckValue;
	else
		// set the check value for all check boxes
		for(var i = 0; i &lt; countCheckBoxes; i++)
			objCheckBoxes[i].checked = CheckValue;
}
&lt;/script&gt;</code></pre>
<p>And Now add this code before &lt;/form&gt; tag. Which add two buttons in your code.</p>
<pre><code> &lt;input type="button" onclick="selectAllCheckBoxes('form1', 'checkbox[]', true);" value="Select All"&gt;
&lt;input type="button" onclick="selectAllCheckBoxes('form1', 'checkbox[]', false);" value="Clear All"&gt; </code></pre>
<p>You have done.</p>
<p>Get Complete Code Here:</p>
<pre><code>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;title&gt;Delete Multiple Records&lt;/title&gt;
&lt;script&gt;
function selectAllCheckBoxes(FormName, FieldName, CheckValue)
{
	if(!document.forms[FormName])
		return;
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if(!objCheckBoxes)
		return;
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes)
		objCheckBoxes.checked = CheckValue;
	else
		// set the check value for all check boxes
		for(var i = 0; i &lt; countCheckBoxes; i++)
			objCheckBoxes[i].checked = CheckValue;
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php
 $dbhost="localhost";
   $dbuser="root";
   $dbpass="";
   $dbname="test";
mysql_connect("$dbhost","$dbuser","$dbpass") or die('Could not connect');
mysql_select_db($dbname);
?&gt;
&lt;form name="form1" method="post" action=""&gt;
&lt;table width="409" border="0" cellpadding="2" cellspacing="0" bordercolor="#999999"&gt;
&lt;tr bgcolor="#CCCCCC"&gt;
&lt;td width="52"&gt;&lt;div align="center"&gt;Select&lt;/div&gt;&lt;/td&gt;
&lt;td width="98"&gt;&lt;div align="center"&gt;Name&lt;/div&gt;&lt;/td&gt;
&lt;td width="142"&gt;&lt;div align="center"&gt;Address&lt;/div&gt;&lt;/td&gt;
&lt;td width="101"&gt;&lt;div align="center"&gt;Contact No&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;?php
$query="SELECT * FROM tbluser";
$result=mysql_query($query);
$sno=1;
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
?&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input name="checkbox[]" type="checkbox" id="checkbox[]" value="&lt;? echo $row['userid']; ?&gt;"&gt;&lt;/td&gt;
&lt;td&gt;&lt;? echo $row['name'];?&gt;&lt;/td&gt;
&lt;td&gt;&lt;? echo $row['address'];?&gt;&lt;/td&gt;
&lt;td&gt;&lt;? echo $row['contactno'];?&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;?
	$sno=$sno+1;
	}
	?&gt;
&lt;tr&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;td colspan="3"&gt;
&lt;div align="right"&gt;
&lt;input name="delete" type="submit" id="delete" value="Delete Records"&gt;
&amp;nbsp;&amp;nbsp;
&lt;input type="button" onclick="selectAllCheckBoxes('form1', 'checkbox[]', true);" value="Select All"&gt;
&amp;nbsp;&amp;nbsp;
&lt;input type="button" onclick="selectAllCheckBoxes('form1', 'checkbox[]', false);" value="Clear All"&gt;
&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;?
if($_POST['delete']){
//print_r($_POST);
$checkbox=$_POST['checkbox'];
//exit;
for($i=0;$i&lt;count($checkbox);$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM tbluser WHERE userid='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo "&lt;meta http-equiv=\"refresh\" content=\"0;URL=deletemultiplerecords.php\"&gt;";
}
}
?&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>
<p><a href="http://www.jagadishwor.com.np/wp-content/uploads/2009/08/deletemultiplerecords_Updated.rar">Download Zip Here</a></p>
<p>Source: www.jagadishwor.com.np<br />
<h3>See Also Following Topics:</h3>
<ul class="related_post">
<li><a href="http://www.themeheven.com/2010/01/10-simple-php-login-sytem-tutorial/" title="10 Simple PHP Login Sytem Tutorial ">10 Simple PHP Login Sytem Tutorial </a></li>
<li><a href="http://www.themeheven.com/2010/01/an-introduction-to-object-oriented-php/" title="An Introduction to Object Oriented PHP">An Introduction to Object Oriented PHP</a></li>
<li><a href="http://www.themeheven.com/2009/10/php-image-file-upload-tutorial/" title="PHP Image File Upload Tutorial">PHP Image File Upload Tutorial</a></li>
<li><a href="http://www.themeheven.com/2009/08/php-redirect-to-another-pageurl-script/" title="PHP Redirect to another Page/URL Script">PHP Redirect to another Page/URL Script</a></li>
<li><a href="http://www.themeheven.com/2009/06/advanced-php-query-function/" title="Advanced PHP Query function">Advanced PHP Query function</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.themeheven.com/2009/08/delete-multiple-records-using-checkbox-with-select-all-buttons/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Advanced PHP Query function</title>
		<link>http://www.themeheven.com/2009/06/advanced-php-query-function/</link>
		<comments>http://www.themeheven.com/2009/06/advanced-php-query-function/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 11:07:41 +0000</pubDate>
		<dc:creator>themeheven</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.themeheven.com/2009/06/advanced-php-query-function/</guid>
		<description><![CDATA[Today’s web applications are becoming more and more advanced, and most of them are relying on data stored in databases. In most cases several queries have to be executed to get the initial data in the application, so a quick way to get this job done would be usefull. Thats why I’ve made this advanced [...]]]></description>
			<content:encoded><![CDATA[<p>Today’s web applications are becoming more and more advanced, and most of them are relying on data stored in databases. In most cases several queries have to be executed to get the initial data in the application, so a quick way to get this job done would be usefull. Thats why I’ve made this advanced PHP query function to use in my own projects, but I’m sure many of you will find it usefull too.</p>
<p>Some of its features are custom SQL parameters like LAST_INSERTED_ID or INSERT_ID_FROM(n), executing different queries in one line of code, quickly returning SELECT queries as arrays, returning the new ID as the query result for INSERT-statements and so on.</p>
<p><strong>Executing several SQL-statements easily</strong></p>
<p>Suppose we need several SELECT-queries to retrieve the data needed for our application to run. Many of you already have made query-functions to retrieve the database resultset in just one line of code, but this probably still creates a new connection for each query you execute. Instead of doing that, we can loop through an array of SQL-statements and execute them all in the same connection. You’ll get the same result, but only one connection to the database is created, which results in a better function performance.</p>
<p>Offcourse we still need to be able to fetch all result-sets returned by our queries. This is where the “$query_results”-array comes in handy. I’ve placed a small example below to clear things up.</p>
<pre><code>// when executing multiple queries, the function will always
// return the result from the last executed SQL-statement
// you can execute one query by just passing the
// SQL-statement as a string, or execute multiple by passing
// an array of SQL-statements.
$sessions = query( array( "SELECT * FROM db_accounts",
	"SELECT * FROM db_hits_np",
	"SELECT * FROM db_sessions_np") );

// we can get the other results through $query_results
// pass the index of the SQL-statement to get its result
// be carefull, the indexes start from 0 !
$accounts = $query_results[1];
$hits_np = $query_results[2];</code></pre>
<p><strong>Getting the result as an associative array</strong></p>
<p>This is basically the same functionality as the mysql_fetch_array() method, but it saves you a line of code. You can specify a second (optional ) parameter when calling the function to specify if you want the result of the query to be returned as a normal resultset or as an associative array.</p>
<p>Set this optional parameter to “true” if you’d like an associative array to be returned, false otherwise. The default value is false</p>
<p><strong>SQL INSERT-statement functions</strong></p>
<p>There are many situations where we need to insert a new record to the database, and right after use the newly inserted ID in our code. We can retrieve the new ID through the SQL “@@IDENTITY” command or PHP’s mysql_insert_id() function, but wouldn’t it be a nice feature to get the ID as a result of every INSERT-statement ? This function does it all for you.</p>
<p><strong>Custom SQL syntax for easier coding with INSERT-statements</strong></p>
<p>It happens quite a lot when we insert several new records in a database, and we need the newly added ID in other SQL statements. Instead of using our query result in our next SQL-statement, we can add a bunch of statements and define in our SQL which ID should come instead of the given parameter. There are 2 custom SQL variables available:</p>
<p><strong>LAST_INSERTED_ID</strong></p>
<p>This parameter returns the last inserted ID, as you could probably guess. Here’s an example:</p>
<pre><code>// we're using the LAST_INSERTED_ID here to
// use the newly returned ID of the last
// INSERT-statement in this connection.
// Don't use this if the last query was not
// an INSERT-statement
query( array( "INSERT INTO db_first_table(name, value, something) VALUES('name', 'value', 'something')",
	"INSERT INTO db_second_table(first_id, value) VALUES(LAST_INSERTED_ID, 'value')" ) );

INSERT_ID_FROM(number)</code></pre>
<p>Less obvious to use is the INSERT_ID_FROM variable. Before executing your query, this variable is replaced by the ID returned from one of the previous SQL INSERT-statements executed in the same database connection. We can define the SQL-statements index in the parameter. Another example:</p>
<pre><code>// we're using the INSERT_ID_FROM here to
// the newly added ID of one of the previous
// INSERT-statements of this connection.
// pass the index of the INSERT-statement
// to the parameter
query( array( "INSERT INTO db_first_table(name, value, something) VALUES('name', 'value', 'something')",
	"INSERT INTO db_second_table(first_id, value) VALUES(LAST_INSERTED_ID, 'value')",
	"INSERT INTO db_third_table(first_id, value) VALUES(INSERT_ID_FROM(0), 'value')" ) );</code></pre>
<p><strong>The Query-function</strong></p>
<p>So here it is, the function I’ve spent 902 words on in this post:</p>
<p>( You can easily download the function in the zip-file attached below )</p>
<pre><code>$c_host = 'localhost';
$c_database = 'database_name';
$c_username = 'username';
$c_password = 'password';

function query($sql, $arr=false){
	// Advanced PHP Query function by Nick Van der Vreken
	// more information about this function on
	// http://www.bydust.com/advanced-php-query-functionadvanced-php-query-function/
	// please leave this comment intact if you're using my function <img src='http://www.themeheven.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> 

	global $c_host, $c_database, $c_username, $c_password, $query_inserted_id, $query_results;

	$query_inserted_id = array();
	$query_results = array();
	$queries = is_array($sql)?$sql:array($sql);
	$conn = mysql_connect( $c_host, $c_username, ($c_password != '')?$c_password:NULL );
	mysql_select_db( $c_database, $conn );
	foreach($queries as $i =&gt; $query){
		if( eregi('LAST_INSERTED_ID', $query) ){ // LAST_INSERTED_ID parameter
			if( count($query_inserted_id) )
				$query = str_replace( 'LAST_INSERTED_ID', $query_inserted_id[count($query_inserted_id) -1], $query );
			else die('Error on LAST_INSERTED_ID, there were no insert-statements executed in the current connection');
		}
		if( eregi('INSERT_ID_FROM', $query) ){ // INSERT_ID_FROM parameter
			$s = strpos( $query, 'INSERT_ID_FROM(');
			$n = substr( $query, $s + 15, 1);
			$id = ( $n &lt; count($query_inserted_id) ) ? $query_inserted_id[$n] : die('Error on INSERT_ID_FROM, wrong index');
			$query = substr( $query, 0, $s ) . $id . substr( $query, strpos( $query, ')', $s) + 1 );
		}
		//echo "&lt;br/&gt;&lt;b&gt;Query $i: &lt;/b&gt; $query";
		$result = mysql_query(  $query , $conn );
		array_push( $query_results, $result);
		array_push( $query_inserted_id, mysql_insert_id($conn) );
		if( ( $i == count($queries) -1 ) &amp;&amp; ( strpos( $query, 'INSERT') === 0 || strpos( $query, 'insert') === 0 ) ) $result = $query_inserted_id[count($query_inserted_id) -1];
	}
	mysql_close($conn);
	if($arr) $result = ( $result &amp;&amp; mysql_num_rows($result) ) ? mysql_fetch_array($result) : NULL;
	return $result;
}</code></pre>
<p>Don’t forget to set the database information in the variables mentioned, otherwise this won’t work !</p>
<p><a href="http://www.bydust.com/advanced-php-query-function/" target="_blank">View Original Post Here</a><br />
<h3>See Also Following Topics:</h3>
<ul class="related_post">
<li><a href="http://www.themeheven.com/2010/01/10-simple-php-login-sytem-tutorial/" title="10 Simple PHP Login Sytem Tutorial ">10 Simple PHP Login Sytem Tutorial </a></li>
<li><a href="http://www.themeheven.com/2010/01/an-introduction-to-object-oriented-php/" title="An Introduction to Object Oriented PHP">An Introduction to Object Oriented PHP</a></li>
<li><a href="http://www.themeheven.com/2009/10/php-image-file-upload-tutorial/" title="PHP Image File Upload Tutorial">PHP Image File Upload Tutorial</a></li>
<li><a href="http://www.themeheven.com/2009/08/php-redirect-to-another-pageurl-script/" title="PHP Redirect to another Page/URL Script">PHP Redirect to another Page/URL Script</a></li>
<li><a href="http://www.themeheven.com/2009/08/delete-multiple-records-using-checkbox-with-select-all-buttons/" title="Delete Multiple Records Using Checkbox With Select All Buttons">Delete Multiple Records Using Checkbox With Select All Buttons</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.themeheven.com/2009/06/advanced-php-query-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- www.000webhost.com Analytics Code -->
<script type="text/javascript" src="http://analytics.hosting24.com/count.php"></script>
<noscript><a href="http://www.hosting24.com/"><img src="http://analytics.hosting24.com/count.php" alt="web hosting" /></a></noscript>
<!-- End Of Analytics Code -->
