<?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>Freelance PHP Developer &#187; mysql developer</title>
	<atom:link href="http://freelance-php-developer.phpmysql.co.za/tag/mysql-developer/feed/" rel="self" type="application/rss+xml" />
	<link>http://freelance-php-developer.phpmysql.co.za</link>
	<description>Freelance PHP Developer &#124; South Africa</description>
	<lastBuildDate>Sat, 07 Jan 2012 06:42:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>mysql_fetch_array</title>
		<link>http://freelance-php-developer.phpmysql.co.za/2010/03/mysql_fetch_array/</link>
		<comments>http://freelance-php-developer.phpmysql.co.za/2010/03/mysql_fetch_array/#comments</comments>
		<pubDate>Sat, 27 Mar 2010 21:03:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Freelance Web Developer]]></category>
		<category><![CDATA[MySQL Tutorials]]></category>
		<category><![CDATA[database developer]]></category>
		<category><![CDATA[database specialist]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[mysql developer]]></category>
		<category><![CDATA[mysql freelancer]]></category>

		<guid isPermaLink="false">http://freelance-php-developer.phpmysql.co.za/?p=54</guid>
		<description><![CDATA[mysql_fetch_array: Why Use It?
Do you know what is returned when you used the mysql_query function to query a MySQL database?  It isn&#8217;t something you can directly manipulate, that is for sure. Here is a sample SELECT query of a table we created in the MySQL Create Table lesson.

PHP and MySQL Code:
&#60;?php
$result = mysql_query("SELECT * [...]


No related posts.

Related posts brought to you by <a href='http://www.wordpressconnect.net/amazonpress/'>Amazon plugin</a>.]]></description>
			<content:encoded><![CDATA[<h1>mysql_fetch_array: Why Use It?</h1>
<p>Do you know what is returned when you used the <em>mysql_query</em> function to query a MySQL database?  It isn&#8217;t something you can directly manipulate, that is for sure. Here is a sample <em>SELECT</em> query of a table we created in the MySQL Create Table lesson.</p>
<div>
<h2>PHP and MySQL Code:</h2>
<pre>&lt;?php
$result = mysql_query("SELECT * FROM example");
?&gt;
</pre>
</div>
<p>The value that <em>mysql_query</em> returns and stores into <em>$result</em> is a special type of data, it is a MySQL Resource. Additional PHP functions are required to extract the data from this Resource.</p>
<h1>A Row of Data</h1>
<p>The <em>mysql_fetch_array</em> function takes a MySQL query resource as an argument (<em>$result</em>) and returns the first row of data returned by the <em>mysql_query</em>.  Our table <em>example</em> basically looks like the table below.</p>
<h2>example MySQL Table:</h2>
<div>
<table border="1">
<tbody>
<tr>
<th>name</th>
<th>age</th>
</tr>
<tr>
<td>Timmy Mellowman</td>
<td>23</td>
</tr>
<tr>
<td>Sandy Smith</td>
<td>21</td>
</tr>
<tr>
<td>Bobby Wallace</td>
<td>15</td>
</tr>
</tbody>
</table>
</div>
<p>The first row of data in this table is &#8220;Timmy Mellowman&#8221; and &#8220;23&#8243;.  When we fetch an array from our MySQL Resource <em>$result</em> it should have Timmy&#8217;s name and age in it.</p>
<h1>Getting a Row of Data using mysql_fetch_array</h1>
<p><em>mysql_fetch_array</em> returns the first row in a MySQL Resource in the form of an associative array.  The columns of the MySQL Result can be accessed  by using the column names of the table.  In our table <em>example</em> these are: name and age.  Here is the code to print out the first MySQL Result row.</p>
<div>
<h2>PHP and MySQL Code:</h2>
<pre>&lt;?php
// Make a MySQL Connection
$query = "SELECT * FROM example"; 

$result = mysql_query($query) or die(mysql_error());

$row = mysql_fetch_array($result) or die(mysql_error());
echo $row['<span style="color: red;">name</span>']. " - ". $row['<span style="color: red;">age</span>'];
?&gt;
</pre>
</div>
<h2>Display:</h2>
<div>Timmy Mellowman &#8211; 23</div>
<p>This is just what we expected would happen!  Now, the cool thing about <em>mysql_fetch_array</em> is that you can use it again on the same MySQL Resource to return the second, third, fourth and so on rows. You can keep doing this until the MySQL Resource has reached the end (which would be three times in our example).</p>
<p>Sounds like an awfully repetitive task. It would be nice if we could get all our results from a MySQL Resource in an easy to do script.</p>
<h1>Fetch Array While Loop</h1>
<p>As we have said, the <em>mysql_fetch_array</em> function returns an associative array, but it <strong>also</strong> returns FALSE if there are no more rows to return!  Using a PHP While Loop we can use this information to our advantage.</p>
<p>If we place the statement &#8220;$row = mysql_fetch_array()&#8221; as our while loop&#8217;s conditional statement we will accomplish two things:</p>
<ol>
<li>We will get a new row of MySQL information that we can print out each time the while loop checks its conditional statement.</li>
<li>When there are no more rows the function will return FALSE causing the while loop to stop!</li>
</ol>
<p>Now that we know what we need to do and how to go about doing it, the code pretty much writes itself, so let&#8217;s move on to the next lesson. Just kidding! Here is the code that will print out all the rows of our MySQL Resource.</p>
<div>
<h2>PHP and MySQL Code:</h2>
<pre>&lt;?php
// Make a MySQL Connection
$query = "SELECT * FROM example"; 

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
	echo $row['name']. " - ". $row['age'];
	echo "&lt;br /&gt;";
}
?&gt;
</pre>
</div>
<h2>Display:</h2>
<div>Timmy Mellowman &#8211; 23<br />
Sandy Smith &#8211; 21<br />
Bobby Wallace &#8211; 15</div>


<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://www.wordpressconnect.net/amazonpress/'>Amazon plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://freelance-php-developer.phpmysql.co.za/2010/03/mysql_fetch_array/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

