<?xml version="1.0" encoding="iso-8859-1"?>
<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>CrankBerry Blog &#187; MySQL</title>
	<atom:link href="http://www.crankberryblog.com/category/scripts-and-programming/mysql-scripts-and-programming/feed" rel="self" type="application/rss+xml" />
	<link>http://www.crankberryblog.com</link>
	<description>Sharing ideas on web design, development and the internet economy</description>
	<lastBuildDate>Thu, 24 Nov 2011 19:37:08 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Installing Sphinx on CentOS/Linux through SSH</title>
		<link>http://www.crankberryblog.com/2011/installing-sphinx-on-centoslinux-through-ssh</link>
		<comments>http://www.crankberryblog.com/2011/installing-sphinx-on-centoslinux-through-ssh#comments</comments>
		<pubDate>Fri, 22 Apr 2011 17:29:00 +0000</pubDate>
		<dc:creator>Jerry Low</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.crankberryblog.com/?p=479</guid>
		<description><![CDATA[Here are the steps into setting up Sphinx for your Plesk sever through SSH. I'm running on CentOS 5.2 but I'm pretty sure these instructions work on most Linux environment. If you're looking to <a href="http://www.crankberryblog.com/2011/intalling-sphinx-on-wamp-localhost-windows">install Sphinx on WAMP</a> (Windows machine) there's another article for that.
]]></description>
			<content:encoded><![CDATA[<p>Here are the steps into setting up Sphinx for your Plesk sever through SSH. I&#039;m running on CentOS 5.2 but I&#039;m pretty sure these instructions work on most Linux environment. If you&#039;re looking to <a href="http://www.crankberryblog.com/2011/intalling-sphinx-on-wamp-localhost-windows">install Sphinx on WAMP</a> (Windows machine) there&#039;s another article for that.</p>
<p>Here is my environment:</p>
<ul>
<li>CentOS 5.2</li>
<li>MySQL 5.1.36</li>
<li>PHP 5.3.6</li>
<li>Sphinx 1.10</li>
</ul>
<p>To do all this, login to your server via SSH with your favourite Telnet/SSH client. I prefer to use <a href="http://www.chiark.greenend.org.uk/~sgtatham/putty/" target="_blank">PuTTY</a>. Oh, you must login as root so have root access!</p>
<h3>How Does Sphinx Works</h3>
<p>In brief this is how Sphinx works. Sphinx creates a separate index of the table you want to search. Your PHP scripts calls the Sphinx API to search connecting to a specific port where Sphinx is listening to. Sphinx then searches its own index and does the search returning the result. Yes, the index has to be updated every time your update your original table. On my live environment I have a <a href="http://www.crankberryblog.com/2010/php-cron-job-without-using-cron-tab-cron-alternative-with-mysql-or-just-txt">cron job setup</a> to reindex with Sphinx every so often.</p>
<h3>Step 1 Extract and Prepare Conf File</h3>
<p>First of all you have to download the Sphinx package and extract it on your server. Here are the command lines for that. Note that the package I&#039;m downloading is the newest (1.10 Beta at this moment). If you&#039;re downloading the older stable version or newer ones since this article is written then you have to change the URL. BTW don&#039;t type in the ($).</p>
<pre>$ wget http://sphinxsearch.com/files/sphinx-1.10-beta.tar.gz
$ tar xzvf sphinx-0.9.8.tar.gz
$ cd sphinx</pre>
<p>To Summarize, the first line downloaded the Sphinx package to your server. Second line untarred it and lastly you went into the directory of your new download.</p>
<p>You can do the configuration if you&#039;ll like to edit the installation process. Here&#039;s an excerpt from Sphinx&#039;s site:</p>
<pre>$ ./configure

There's a number of options to configure. The complete listing may be
obtained by using --help switch. The most important ones are:

--prefix, which specifies where to install Sphinx; such as
--prefix=/usr/local/sphinx (all of the examples use this prefix)
--with-mysql, which specifies where to look for MySQL include and
library files, if auto-detection fails;
--with-pgsql, which specifies where to look for PostgreSQL include
and library files.</pre>
<p>Once you&#039;re done then all you have to do is make the binaries and install the software:</p>
<pre>$ make
$ make install</pre>
<p>Once you have installed Sphinx you&#039;ll need to prepare the configuration file. This is the most important part as Sphinx actually acts based on the settings and parameters defined in this single document. Let&#039;s say we&#039;ve installed Sphinx into /usr/local/sphinx. Copy the sample configuration file from the downloaded package into the Sphinx installation folder:</p>
<pre>mv /sphinx/sphinx-min.conf.in /usr/local/sphinx/sphinx.conf </pre>
<p>I named it sphinx.conf for ease, but you can call it anything you want. Now you want to edit sphinx.conf with your favourite editor. I like to use VI editor. Here&#039;s what the initial open of the file looks like and I&#039;ll run through each section of this script.</p>
<pre>#
# Minimal Sphinx configuration sample (clean, simple, functional)
#

source src1
{
	type			= mysql

	sql_host		= localhost
	sql_user		= test
	sql_pass		=
	sql_db			= test
	sql_port		= 3306	# optional, default is 3306

	sql_query		= \
		SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS
		date_added, title, content \
		FROM table

	sql_attr_uint		= group_id
	sql_attr_timestamp	= date_added

	sql_query_info		= SELECT * FROM table WHERE id=$id
}

index test1
{
	source			= src1
	path			= @CONFDIR@/data/test1
	docinfo			= extern
	charset_type		= sbcs
}

index testrt
{
	type			= rt
	rt_mem_limit		= 32M

	path			= @CONFDIR@/data/testrt
	charset_type		= utf-8

	rt_field		= title
	rt_field		= content
	rt_attr_uint		= gid
}

indexer
{
	mem_limit		= 32M
}

searchd
{
	listen			= 9312
	listen			= 9306:mysql41
	log			= @CONFDIR@/log/searchd.log
	query_log		= @CONFDIR@/log/query.log
	read_timeout		= 5
	max_children		= 30
	pid_file		= @CONFDIR@/log/searchd.pid
	max_matches		= 1000
	seamless_rotate		= 1
	preopen_indexes		= 0
	unlink_old		= 1
	workers			= threads # for RT to work
}</pre>
<p>These settings should cover most of what you&#039;re looking for in terms of setting up Sphinx. I&#039;ll try to run through everything as much as possible. There are more parameters and customization you can do by reading their documentation here.</p>
<p><strong>source src1</strong></p>
<p>This section defines your SQL settings and what is needed to be index. So the first few lines sql_host, sql_user, sql_pass, sql_db is exactly your local host MySQL settings. The sql_port is defaulted to 3306. I haven&#039;t changed this setting for Wamp so that&#039;s where my port sits.</p>
<p>The sql_query is actually crucial. This is the query that you want Sphinx to do and build an index on. Its just like an SQL query you would do. Say my table people had columns: id, name, address, city, age, and joined (stored as time stamp) as follow:</p>
<table width="520" border="1" cellspacing="2" cellpadding="2">
<tr>
<td>id</td>
<td>name</td>
<td>address</td>
<td>city</td>
<td>age</td>
<td>joined</td>
</tr>
<tr>
<td>50</td>
<td>john doe</td>
<td>546 random st.</td>
<td>Vancouver</td>
<td>42</td>
<td>124568794562</td>
</tr>
<tr>
<td>51</td>
<td>amanda smith</td>
<td>
<p>3425 great ave</p>
</td>
<td>Vancouver</td>
<td>18</td>
<td>124568923423</td>
</tr>
<tr>
<td>52</td>
<td>justin heiber</td>
<td>352 awesome dr</td>
<td>
<p>Burnaby</p>
</td>
<td>56</td>
<td>124569000231</td>
</tr>
<tr>
<td>53</td>
<td>macy store</td>
<td>355 loser pl</td>
<td>Richmond</td>
<td>26</td>
<td>124569000352</td>
</tr>
</table>
<p>I want to be able to search for the people&#039;s name and address only so thats all I care about, but I have to include the id and the timestamp. You can also defined unsigned integer attributes. So my sql_query will be:</p>
<pre>sql_query		= \
		SELECT id, id AS id_attr, name, address, joined  \
		FROM people

sql_attr_uint		= id_attr
sql_attr_timestamp	= joined

sql_query_info		= SELECT * FROM people WHERE id=$id</pre>
<p>I&#039;ve defined the sql_attr_uint, sql_attr_timestamp, and sql_query_info. The sql_attr_uint is the unsigned integer attribute and it can&#039;t be the first column which is to define your id. The sql_attr_timestamp is the UNIX timestamp in your column. You can convert if you want but I usually store my dates in timestamp anyways. The sql_query_info is for testing purpose only. It is the query that retrieves the actual row after searching. You have to do this yourself in your PHP Script.</p>
<p><strong>index test1</strong></p>
<p>You can change the name of test1 to anything you want but for the sake of it I&#039;ll just leave it. The source is just the name of the section we did above. If you changed the name then you have to change it here. The path is where you want to store the indexed file on your site. So I&#039;ll store it in the site directory in a folder called sphinx: /var/www/vhosts/site.com/sphinx/test1. The remainder I left the same. I&#039;ve taken out the real time indexing because I&#039;ll be doing my own thing and I heard there were lots of limitations to it at this point, so its not my thing. If it floats your boat you can do some more research and look into it. Here&#039;s my index:</p>
<pre>index test1
{
	source			= src1
	path			= /var/sphinx/test1
	docinfo			= extern
	charset_type		= sbcs
}
indexer</pre>
<p>This section defines the indexer. My database is big so I increased the mem_limit to 64M. Don&#039;t just go for a high or low number. Too low can hurt your searching and too high can hurt your server. Find the balance based on the size of your database.</p>
<p><strong>searchd</strong></p>
<p>This part defined the searchd which is the search application from Sphinx. Besides the log paths I didn&#039;t change anything else. The listen port is where you want searchd to listen in on for search queries. I left it at 9312 which is where I&#039;ll point my PHP script to later on.</p>
<p>Here is my sphinx.conf file:</p>
<pre>#
# Minimal Sphinx configuration sample (clean, simple, functional)
#

source src1
{
	type			= mysql

	sql_host		= localhost
	sql_user		= test
	sql_pass		=
	sql_db			= test
	sql_port		= 3306	# optional, default is 3306

	sql_query		= \
		SELECT id, id AS id_attr, name, address, joined  \
		FROM people

	sql_attr_uint		= id_attr
	sql_attr_timestamp	= joined

	sql_query_info		= SELECT * FROM people WHERE id=$id
}

index test1
{
	source			= src1
	path			= /var/sphinx/test1
	docinfo			= extern
	charset_type		= sbcs
}

indexer
{
	mem_limit		= 64M
}

searchd
{
	listen			= 9312
	listen			= 9306:mysql41
	log			= /var/log/sphinx/test1searchd.log
	query_log		= /var/log/sphinx/test1query.log
	read_timeout		= 5
	max_children		= 30
	pid_file		= /var/log/sphinx/test1searchd.pid
	max_matches		= 1000
	seamless_rotate		= 1
	preopen_indexes		= 0
	unlink_old		= 1
}</pre>
<h3>Getting Sphinx Ready</h3>
<p>Now that you have the conf file setted up, save and close the file. Now we&#039;re ready to install.</p>
<p>First, we&#039;re going to build the index by typing in <em>indexer -config /usr/local/sphinxsphinx.conf test1</em>. Replace test1 if you changed the name of your index. If you don&#039;t get any errors and see some numbers run then you&#039;re fine.</p>
<p>Once the index is build you want to prepare Sphinx&#039;s service on your machine. Type in searchd -install -config /usr/local/sphinx/sphinx.conf. It&#039;ll prompt you that the installation was successful.</p>
<h3>Testing Sphinx Out</h3>
<p>Let us try out the new nifty search. So say I want to search my table for john doe I&#039;ll type search /usr/local/sphinx/sphinx.conf &#034;john doe&#034;. That should return something for me. I can define the matching modes and which column to search as well such as search -ext2 -config /usr/local/sphinx/sphinx.conf &#034;@name john @address awesome&#034;. If you are getting the results you want then you are ready to move forward into your PHP script.</p>
<h3>Sphinx and Your PHP Script</h3>
<p>You need the Sphinx API in your script for it to work so go to c:\sphinx\api and copy sphinxapi.php to your site directory. Include this file in your script and call on the class:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">require_once</span><span class="br0">&#40;</span><span class="st0">&#039;sphinxapi.php&#039;</span><span class="br0">&#41;</span>;<br />
<span class="co1">//Sphinx</span><br />
<span class="re0">$s</span> = <span class="kw2">new</span> SphinxClient;<br />
<span class="re0">$s</span>-&gt;<span class="me1">setServer</span><span class="br0">&#40;</span><span class="st0">&quot;localhost&quot;</span>, <span class="nu0">9312</span><span class="br0">&#41;</span>;<br />
<span class="re0">$s</span>-&gt;<span class="me1">setMatchMode</span><span class="br0">&#40;</span>SPH_MATCH_EXTENDED2<span class="br0">&#41;</span>;</div>
<p>Noticed I have setServer pointing to the port I told Sphinx to listen to. The setMatchMode is just the matching mode you&#039;ll like to use. In my case I&#039;m using extended 2.</p>
<p>Now you can query search your Sphinx index with:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="co1">//Search Query</span><br />
<span class="re0">$result</span> = <span class="re0">$s</span>-&gt;<span class="me1">query</span><span class="br0">&#40;</span><span class="st0">&quot;@name $searchName @address $searchAddress&quot;</span><span class="br0">&#41;</span>;</div>
<p>The $result returned will not include the rows and columns of your actual table. It&#039;ll return the follow parameters (taken from <a href="http://www.php.net" target="_blank">php.net</a>):</p>
<table width="520" border="1" cellpadding="1" cellspacing="1">
<thead valign="middle">
<tr valign="middle">
<th>Key</th>
<th>Value description</th>
</tr>
</thead>
<tbody valign="middle">
<tr valign="middle">
<td align="left">&quot;matches&quot;</td>
<td align="left">An array with found document IDs as keys and their weight and attributes values as values</td>
</tr>
<tr valign="middle">
<td align="left">&quot;total&quot;</td>
<td align="left">Total number of matches found and retrieved (depends on your settings)</td>
</tr>
<tr valign="middle">
<td align="left">&quot;total_found&quot;</td>
<td align="left">Total number of found documents matching the query</td>
</tr>
<tr valign="middle">
<td align="left">&quot;words&quot;</td>
<td align="left">An array with words (case-folded and stemmed) as keys and per-word statistics as values</td>
</tr>
<tr valign="middle">
<td align="left">&quot;error&quot;</td>
<td align="left">Query error message reported by searchd</td>
</tr>
<tr valign="middle">
<td align="left">&quot;warning&quot;</td>
<td align="left">Query warning reported by searchd</td>
</tr>
</tbody>
</table>
<p>This means that $result['total'] will tell me how many results were found and $result['matches'] is where the id of the search results are stored. So here&#039;s a simple line to return all of the results:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$result</span><span class="br0">&#91;</span><span class="st0">&#039;total&#039;</span><span class="br0">&#93;</span> &gt; <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">foreach</span> <span class="br0">&#40;</span><span class="re0">$result</span><span class="br0">&#91;</span><span class="st0">&#039;matches&#039;</span><span class="br0">&#93;</span> <span class="kw1">as</span> <span class="re0">$id</span> =&gt; <span class="re0">$otherStuff</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Get Column</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$searchColumn</span> = <a href="http://www.php.net/mysql_fetch_array"><span class="kw3">mysql_fetch_array</span></a><span class="br0">&#40;</span> <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span><span class="st0">&quot;SELECT * FROM people WHERE id=$id&quot;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Dump</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/var_dump"><span class="kw3">var_dump</span></a><span class="br0">&#40;</span><span class="re0">$searchColumn</span><span class="br0">&#41;</span>; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#039;No results found&#039;</span>; &nbsp; &nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span></div>
<p>Notice that the mysql_query is the same as sql_query_info in sphinx.conf. What a coincidence eh? LOL. That is all and now you have a working Sphinx search system setup on your local machine. If you need to reindex your Sphinx you can do so without restarting the service. In command prompt just run <em>indexer -config /usr/local/sphinx/sphinx.conf -rotate test1</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crankberryblog.com/2011/installing-sphinx-on-centoslinux-through-ssh/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Intalling Sphinx on Wamp localhost (Windows)</title>
		<link>http://www.crankberryblog.com/2011/intalling-sphinx-on-wamp-localhost-windows</link>
		<comments>http://www.crankberryblog.com/2011/intalling-sphinx-on-wamp-localhost-windows#comments</comments>
		<pubDate>Sun, 03 Apr 2011 20:42:39 +0000</pubDate>
		<dc:creator>Jerry Low</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Hosting]]></category>

		<guid isPermaLink="false">http://www.crankberryblog.com/?p=458</guid>
		<description><![CDATA[I recall the first time trying to setup Sphinx search on my Windows machine with Wamp it wasn't so obvious of how it was to be setup. Here's a guide in how to get it setup on you local machine. If you want to <a href="http://www.crankberryblog.com/2011/installing-sphinx-on-centoslinux-through-ssh">install Sphinx on Linux / Plesk</a> there's an article for that too.]]></description>
			<content:encoded><![CDATA[<p>I recall the first time trying to setup Sphinx search on my Windows machine with Wamp it wasn&#039;t so obvious of how it was to be setup. Here&#039;s a guide in how to get it setup on you local machine. If you want to <a href="http://www.crankberryblog.com/2011/installing-sphinx-on-centoslinux-through-ssh">install Sphinx on Linux / Plesk</a> there&#039;s an article for that too.</p>
<p>Here is my environment:</p>
<ul>
<li>Windows Vista</li>
<li>Wamp 2.0</li>
<li>Apache 2.2.11</li>
<li>MySQL 5.1.36</li>
<li>PHP 5.3.0</li>
<li>Sphinx 1.10</li>
</ul>
<p>First of all download Sphinx for Windows which can be found (<a href="http://sphinxsearch.com/downloads/beta/">http://sphinxsearch.com/downloads/beta/</a>). I downloaded Win32 binaries w/MySQL support version because I only work with MySQL. Once you got this you&#039;re ready to begin.</p>
<h3>How Does Sphinx Works</h3>
<p>In brief this is how Sphinx works. Sphinx creates a separate index of the table you want to search. Your PHP scripts calls the Sphinx API to search connecting to a specific port where Sphinx is listening to. Sphinx then searches its own index and does the search returning the result. Yes, the index has to be updated every time your update your original table. On my live environment I have a <a href="http://www.crankberryblog.com/2009/setting-php-cron-job-with-crontab">cron job</a> setup to reindex with Sphinx every so often.</p>
<h3>Step 1 Extract and Prepare Conf File</h3>
<p>Extract the files into any folder you want, I&#039;ve selected c:\sphinx\. The next thing you need to do is create a document in c:\sphinx\bin called sphinx.conf. This is the most important part as Sphinx actually acts based on the settings and parameters defined in this single document. Once you have created sphinx.conf in the bin folder copy the content from c:\sphinx\sphinx-min.conf.in (this is their provided sample file). It looks like:</p>
<pre>#
# Minimal Sphinx configuration sample (clean, simple, functional)
#

source src1
{
	type			= mysql

	sql_host		= localhost
	sql_user		= test
	sql_pass		=
	sql_db			= test
	sql_port		= 3306	# optional, default is 3306

	sql_query		= \
		SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS
		date_added, title, content \
		FROM table

	sql_attr_uint		= group_id
	sql_attr_timestamp	= date_added

	sql_query_info		= SELECT * FROM table WHERE id=$id
}

index test1
{
	source			= src1
	path			= @CONFDIR@/data/test1
	docinfo			= extern
	charset_type		= sbcs
}

index testrt
{
	type			= rt
	rt_mem_limit		= 32M

	path			= @CONFDIR@/data/testrt
	charset_type		= utf-8

	rt_field		= title
	rt_field		= content
	rt_attr_uint		= gid
}

indexer
{
	mem_limit		= 32M
}

searchd
{
	listen			= 9312
	listen			= 9306:mysql41
	log			= @CONFDIR@/log/searchd.log
	query_log		= @CONFDIR@/log/query.log
	read_timeout		= 5
	max_children		= 30
	pid_file		= @CONFDIR@/log/searchd.pid
	max_matches		= 1000
	seamless_rotate		= 1
	preopen_indexes		= 0
	unlink_old		= 1
	workers			= threads # for RT to work
}</pre>
<p>These settings should cover most of what you&#039;re looking for in terms of setting up Sphinx. I&#039;ll try to run through everything as much as possible. There are more parameters and customization you can do by reading their documentation <a href="http://sphinxsearch.com/docs/1.10/">here</a>.</p>
<p><strong>source src1</strong></p>
<p>This section defines your SQL settings and what is needed to be index. So the first few lines sql_host, sql_user, sql_pass, sql_db is exactly your local host MySQL settings. The sql_port is defaulted to 3306. I haven&#039;t changed this setting for Wamp so that&#039;s where my port sits.</p>
<p>The sql_query is actually crucial. This is the query that you want Sphinx to do and build an index on. Its just like an SQL query you would do. Say my table people had columns: id, name, address, city, age, and joined (stored as time stamp) as follow: </p>
<table width="520" border="1" cellspacing="2" cellpadding="2">
<tr>
<td>id</td>
<td>name</td>
<td>address</td>
<td>city</td>
<td>age</td>
<td>joined</td>
</tr>
<tr>
<td>50</td>
<td>john doe</td>
<td>546 random st.</td>
<td>Vancouver</td>
<td>42</td>
<td>124568794562</td>
</tr>
<tr>
<td>51</td>
<td>amanda smith</td>
<td>
<p>3425 great ave</p>
</td>
<td>Vancouver</td>
<td>18</td>
<td>124568923423</td>
</tr>
<tr>
<td>52</td>
<td>justin heiber</td>
<td>352 awesome dr</td>
<td>
<p>Burnaby</p>
</td>
<td>56</td>
<td>124569000231</td>
</tr>
<tr>
<td>53</td>
<td>macy store</td>
<td>355 loser pl</td>
<td>Richmond</td>
<td>26</td>
<td>124569000352</td>
</tr>
</table>
<p>I want to be able to search for the people&#039;s name and address only so thats all I care about, but I have to include the id and the timestamp. You can also defined unsigned integer attributes. So my sql_query will be:</p>
<pre>sql_query		= \
		SELECT id, id AS id_attr, name, address, joined  \
		FROM people

sql_attr_uint		= id_attr
sql_attr_timestamp	= joined

sql_query_info		= SELECT * FROM people WHERE id=$id</pre>
<p>I&#039;ve defined the sql_attr_uint, sql_attr_timestamp, and sql_query_info. The sql_attr_uint is the unsigned integer attribute and it <strong>can&#039;t</strong> be the first column which is to define your id. The sql_attr_timestamp is the UNIX timestamp in your column. You can convert if you want but I usually store my dates in timestamp anyways. The sql_query_info is for testing purpose only. It is the query that retrieves the actual row after searching. You have to do this yourself in your PHP Script.</p>
<p><strong>index test1</strong></p>
<p>You can change the name of test1 to anything you want but for the sake of it I&#039;ll just leave it. The source is just the name of the section we did above. If you changed the name then you have to change it here. The path is where you want to store the indexed file on your site. So I&#039;ll store it in the site directory in a folder called sphinx: C:\Wamp\www\site\sphinx\test1. The remainder I left the same. I&#039;ve taken out the real time indexing because I&#039;ll be doing my own thing and I heard there were lots of limitations to it at this point, so its not my thing. If it floats your boat you can do some more research and look into it. Here&#039;s my index:</p>
<pre>index test1
{
	source			= src1
	path			= C:\Wamp\www\site\sphinx\test1
	docinfo			= extern
	charset_type		= sbcs
}</pre>
<p><strong>indexer</strong></p>
<p>This section defines the indexer. My database is big so I increased the mem_limit to 64M. Don&#039;t just go for a high or low number. Too low can hurt your searching and too high can hurt your server. Find the balance based on the size of your database.</p>
<p><strong>searchd</strong></p>
<p>This part defined the searchd which is the search application from Sphinx. Besides the log paths I didn&#039;t change anything else. The listen port is where you want searchd to listen in on for search queries. I left it at 9312 which is where I&#039;ll point my PHP script to later on.</p>
<p>Here is my sphinx.conf file:</p>
<pre>#
# Minimal Sphinx configuration sample (clean, simple, functional)
#

source src1
{
	type			= mysql

	sql_host		= localhost
	sql_user		= test
	sql_pass		=
	sql_db			= test
	sql_port		= 3306	# optional, default is 3306

	sql_query		= \
		SELECT id, id AS id_attr, name, address, joined  \
		FROM people

	sql_attr_uint		= id_attr
	sql_attr_timestamp	= joined

	sql_query_info		= SELECT * FROM people WHERE id=$id
}

index test1
{
	source			= src1
	path			= C:\Wamp\www\s\sphinx\test1
	docinfo			= extern
	charset_type		= sbcs
}

indexer
{
	mem_limit		= 64M
}

searchd
{
	listen			= 9312
	listen			= 9306:mysql41
	log			= C:\Wamp\www\s\sphinx\searchd.log
	query_log		= C:\Wamp\www\s\sphinx\query.log
	read_timeout		= 5
	max_children		= 30
	pid_file		= C:\Wamp\www\s\sphinx\searchd.pid
	max_matches		= 1000
	seamless_rotate		= 1
	preopen_indexes		= 0
	unlink_old		= 1
}</pre>
<h3>Getting Sphinx Ready</h3>
<p>Now that you have prepared the Configuration file which is the hardest part of this then you&#039;re ready to begin. The first part you want to do is prepare the Sphinx index. Open up your command promt (Start > Search [cmd] > CMD.</p>
<p>In command promt navigate to your Sphinx bin folder by typing in <em>cd c:\sphinx\bin</em>. </p>
<p>Once there launched the indexer to build the index by typing in <em>indexer.exe &#8211;config c:\sphinx\bin\sphinx.conf test1</em>. Replace test1 if you changed the name of your index. If you don&#039;t get any errors and see some numbers run then you&#039;re fine. If you get a &#034;failed to write&#034; message it&#039;s because you don&#039;t have admin access to your machine.</p>
<p>Once the index is build you want to install Sphinx on your Windows machine as a service by typing <em>searchd.exe &#8211;install &#8211;config c:\sphinx\bin\sphinx.conf &#8211;servicename Sphinx</em>. It&#039;ll prompt you that the installation was successful.</p>
<p>Now you have to start the service you just installed. Go to control panel > administrative tools > services. Find Sphinx in the list. Click on it and on the left side you should see Start (on my machine that&#039;s where it is). Click on it and it should start.</p>
<h3>Testing Sphinx Out</h3>
<p>Go back to command prompt and test it out using search. So say I want to search my table for john doe I&#039;ll type <em>search sphinx.conf &#034;john doe&#034;</em>. That should return something for me. I can define the <a href="http://sphinxsearch.com/docs/1.10/matching-modes.html">matching modes</a> and which column to search as well such as <em>search &#8211;ext2 &#8211;config sphinx.conf &#034;@name john @address awesome&#034;</em>. If you are getting the results you want then you are ready to move forward into your PHP script.</p>
<h3>Sphinx and Your PHP Script</h3>
<p>You need the Sphinx API in your script for it to work so go to c:\sphinx\api and copy sphinxapi.php to your site directory. Include this file in your script and call on the class:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">require_once</span><span class="br0">&#40;</span><span class="st0">&#039;sphinxapi.php&#039;</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">//Sphinx</span><br />
<span class="re0">$s</span> = <span class="kw2">new</span> SphinxClient;<br />
<span class="re0">$s</span>-&gt;<span class="me1">setServer</span><span class="br0">&#40;</span><span class="st0">&quot;localhost&quot;</span>, <span class="nu0">9312</span><span class="br0">&#41;</span>;<br />
<span class="re0">$s</span>-&gt;<span class="me1">setMatchMode</span><span class="br0">&#40;</span>SPH_MATCH_EXTENDED2<span class="br0">&#41;</span>;</div>
<p>Noticed I have setServer pointing to the port I told Sphinx to listen to. The setMatchMode is just the matching mode you&#039;ll like to use. In my case I&#039;m using extended 2.</p>
<p>Now you can query search your Sphinx index with:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="co1">//Search Query</span><br />
<span class="re0">$result</span> = <span class="re0">$s</span>-&gt;<span class="me1">query</span><span class="br0">&#40;</span><span class="st0">&quot;@name $searchName @address $searchAddress&quot;</span><span class="br0">&#41;</span>;</div>
<p>The $result returned will not include the rows and columns of your actual table. It&#039;ll return the follow parameters (taken from <a href="http://php.net/manual/en/sphinxclient.query.php">php.net</a>):</p>
<table width="520" border="1" cellpadding="1" cellspacing="1">
<thead valign="middle">
<tr valign="middle">
<th>Key</th>
<th>Value description</th>
</tr>
</thead>
<tbody valign="middle">
<tr valign="middle">
<td align="left">&quot;matches&quot;</td>
<td align="left">An array with found document IDs as keys and their weight and attributes values as values</td>
</tr>
<tr valign="middle">
<td align="left">&quot;total&quot;</td>
<td align="left">Total number of matches found and retrieved (depends on your settings)</td>
</tr>
<tr valign="middle">
<td align="left">&quot;total_found&quot;</td>
<td align="left">Total number of found documents matching the query</td>
</tr>
<tr valign="middle">
<td align="left">&quot;words&quot;</td>
<td align="left">An array with words (case-folded and stemmed) as keys and per-word statistics as values</td>
</tr>
<tr valign="middle">
<td align="left">&quot;error&quot;</td>
<td align="left">Query error message reported by searchd</td>
</tr>
<tr valign="middle">
<td align="left">&quot;warning&quot;</td>
<td align="left">Query warning reported by searchd</td>
</tr>
</tbody>
</table>
<p>This means that $result['total'] will tell me how many results were found and $result['matches'] is where the id of the search results are stored. So here&#039;s a simple line to return all of the results:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$result</span><span class="br0">&#91;</span><span class="st0">&#039;total&#039;</span><span class="br0">&#93;</span> &gt; <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">foreach</span> <span class="br0">&#40;</span><span class="re0">$result</span><span class="br0">&#91;</span><span class="st0">&#039;matches&#039;</span><span class="br0">&#93;</span> <span class="kw1">as</span> <span class="re0">$id</span> =&gt; <span class="re0">$otherStuff</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Get Column</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$searchColumn</span> = <a href="http://www.php.net/mysql_fetch_array"><span class="kw3">mysql_fetch_array</span></a><span class="br0">&#40;</span> <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span><span class="st0">&quot;SELECT * FROM people WHERE id=$id&quot;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Dump</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/var_dump"><span class="kw3">var_dump</span></a><span class="br0">&#40;</span><span class="re0">$searchColumn</span><span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#039;No results found&#039;</span>;&nbsp; &nbsp; &nbsp; &nbsp; <br />
<span class="br0">&#125;</span></div>
<p>Notice that the mysql_query is the same as sql_query_info in sphinx.conf. What a coincidence eh? LOL. That is all and now you have a working Sphinx search system setup on your local machine. If you need to reindex your Sphinx you can do so without restarting the service. In command prompt just run <em>indexer.exe &#8211;config c:\sphinx\bin\sphinx.conf &#8211;rotate test1.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crankberryblog.com/2011/intalling-sphinx-on-wamp-localhost-windows/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>The ultimate newb&#039;s guide to learning web development (Pt. 1: The Basic)</title>
		<link>http://www.crankberryblog.com/2010/the-ultimate-newbs-guide-to-learning-web-development-pt-1-the-basic</link>
		<comments>http://www.crankberryblog.com/2010/the-ultimate-newbs-guide-to-learning-web-development-pt-1-the-basic#comments</comments>
		<pubDate>Fri, 24 Sep 2010 22:22:21 +0000</pubDate>
		<dc:creator>Jerry Low</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.crankberryblog.com/?p=406</guid>
		<description><![CDATA[If you are just discovering the world of web development but know nothing about HTML code, css and mumble-jumble, here's a good place to start. In this article I will outline in general where to find resources and some good skills to pick up on to becoming a minor expert in the field.]]></description>
			<content:encoded><![CDATA[<p>If you are just discovering the world of web development but know nothing about HTML code, css and mumble-jumble, here&#039;s a good place to start. In this article I will outline in general where to find resources and some good skills to pick up on to becoming a minor expert in the field.</p>
<p>To be honest, web development is all about practice and understanding, of course creativity as well (I&#039;ll get to this later). Here&#039;s a little scope about how I picked up on web development. I started making websites through FrontPage when I was 13, using only a bit of HTML and mainly the WYSIWYG (what you see is what you get) editor. It wasn&#039;t until I was 15 that I started developing with HTML and using CSS. When I was 18 I felt the need for more power in my websites which is when I picked up PHP and MySQL. About a year ago was finally when I started picking up on Javascript.</p>
<p>Basically, I learned everything through every resource except taking classes. I would search through the web, read books from the library or just pure trial and error. It wasn&#039;t insanely hard to gather all the information but the only times when I really learn was through projects and experiments. Web development is definitely a hands-on type of thing. Point is, if you want to learn web development you&#039;ll have to spend 30% reading and 70% doing.</p>
<h2>The Program/Software</h2>
<p>Web development can be accomplished through a few languages and most of them don&#039;t require any special software to write. The basic notepad that came with your Windows is capable of completing the task. I prefer to use a software that color codes coding to mitigate headaches. Here&#039;s a list of software that you could choose from.</p>
<ul>
<li><a href="http://notepad-plus-plus.org/" target="_blank">Notepad++ (Free)</a></li>
<li><a href="http://kompozer.net/" target="_blank">KompoZer (Free)</a></li>
<li><a href="http://www.w3.org/Amaya/" target="_blank">Amaya (Free)</a></li>
<li><a href="http://www.seamonkey-project.org/" target="_blank">SeaMonkey (Free)</a></li>
<li><a href="http://www.microsoft.com/expression/" target="_blank">Microsoft Expression Studio ($149)</a></li>
<li><a href="http://www.adobe.com/products/dreamweaver/" target="_blank">Adobe Dreamweaver ($399)</a></li>
</ul>
<p>I personally use Dreamweaver on my computer as I have been using it since I was young so I have a nostalgic feeling to it. When I&#039;m on the go I have notepad++ on my USB.</p>
<h2>Learning HTML</h2>
<p>Before you learn anything else, you have to be really good at HTML (hyper text markup language). The basic of websites all boils down to this language. HTML is the markup language containing markup tags that creates the webpage document. Every website requires HTML to make it work, yes even Flash websites.</p>
<p>Your basic website is wrapped in an opening and closing HTML tag. Opening tags are surrounded by arrow brackets while closing tags are the same but there&#039;s a front slash immediately after the first arrow bracket like such</p>
<div class="dean_ch" style="white-space: wrap;">&lt;html&gt;&lt;/html&gt;</div>
<p>Within the HTML tags there will be a HEAD and BODY tag. The head tags will describe items related to the website but will not be produced in the content. Items such as site title, meta tags, CSS, and Javascripts can be defined in the HEAD tag. The BODY tag will describe content that will be outputted to the user. A code like this:</p>
<div class="dean_ch" style="white-space: wrap;">&lt;html&gt;<br />
&lt;head&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;title&gt;Test Page&lt;/title&gt;<br />
&lt;/head&gt;</p>
<p>&lt;body&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Hello world!<br />
&lt;/body&gt;<br />
&lt;/html&gt;</div>
<p>Will output &#034;Hello world!&#034; on the browser with the title Test Page in the browser title bar. Once you get this down you&#039;re ready to start building the website. The goal following this is to output the content you wish to show, it could be paragraphed text, images or tables. Everything you wished to use requires a markup tag to make it work. It&#039;ll be redundant for me to give you a tutorial on all the tags but here are two resources that are great.</p>
<ul>
<li><a href="http://www.w3schools.com/html/default.asp" target="_blank">w3school &#8211; HTML</a></li>
<li><a href="http://www.html.net/tutorials/html/" target="_blank">HTML.net</a></li>
</ul>
<p>w3school is great, as they are short writeups but concise so you can digest all of the content quickly. HTML.net is more detailed and a great read too but they incorporate a bit of CSS so its better to read it after w3school to cross-reference some of the ideas and undestanding. Once you&#039;ve got this down you&#039;re ready for some CSS.</p>
<h2>Learning CSS</h2>
<p>CSS (Cascading Style Sheet) is a language that focuses on the style and layout of your markup language. Its a common standard now generate the accurate aesthetic results of many websites. There are a few ways to include CSS in your website.</p>
<div class="dean_ch" style="white-space: wrap;">//Link to a CSS file &#8211; Goes in the &lt;head&gt;&lt;/head&gt; tag<br />
&lt;link href=&quot;css/styles.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;</p>
<p>//Have the CSS in the &lt;head&gt;&lt;/head&gt; tag<br />
&lt;style type=&quot;text/css&quot;&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; .css { font-weight: bold; }<br />
&lt;/style&gt;</p>
<p>//Inline with content markup tags &#8211; in &lt;body&gt;&lt;/body&gt; tag<br />
&lt;p style=&quot;font-size: 22px;&quot;&gt;this is a paragraph&lt;/p&gt;<br />
&nbsp;</div>
<p>Once you have your website linked with some CSS you can start controlling your website. Here are some example of CSS controlling a webpage.</p>
<div class="dean_ch" style="white-space: wrap;">//All paragraph content will be bold and have a line height of 22 pixels<br />
p { font-weight: bold; line-height: 22px; }</p>
<p>//The following code will modify all markup tags with the class &quot;change&quot; to have a background color of black<br />
.change { background: #000; }</p>
<p>//The following code will modify the element with ID &quot;element_1&quot; to have a background color of white<br />
#element_1 { background: #FFF; }</div>
<p>Here are the resources to read to learn all about CSS.</p>
<ul>
<li><a href="http://www.w3schools.com/css/default.asp" target="_blank">w3school &#8211; CSS</a></li>
<li><a href="http://www.html.net/tutorials/css/" target="_blank">HTML.net</a></li>
</ul>
<p>Again its good to go over the basics with w3school first then head to HTML.net. </p>
<p>At this point you should practice making a few websites with HTML and CSS. Try to envision something and then build it. The best practice is to try to design something in Photoshop then try to build a web site from it. As I always tell developers, just design it and I&#039;ll make it into a template, because I don&#039;t believe there&#039;s many design limitations that can&#039;t be built with HTML and CSS. By doing this exercise it&#039;ll help you learn different methods of building different elements.</p>
<h2>Notes and Reminders</h2>
<p>There are some things you should keep in mind when building with HTML and CSS though.</p>
<h3>Using tables for design</h3>
<p>This is a no and not recommended. A lot of people argue over this topic. Some believe that tables are good for design because its easier to control and it holds the shape of the original design better. The problem is tables are clunky, heavy in coding and not very flexible. Tables have specific standards to them so if you have two rows and two columns, the columns have to be equal width while rows have to be equal height. In order to work around that you&#039;ll have to nest your tables, imagine dealing with that. Its also less flexible for future expansions. You should use a table for tabular data though. Extreme non-table believers will argue that you don&#039;t, they can produce the same effect with other elements. They sure can but when it comes down to the user extracting your data (copying to word or excel), copying for a table is much easier than anything else. It just makes sense.</p>
<h3>Avoid absolute positioning of everything</h3>
<p>Many developers when they start will notice the amount of control with absolute positioning. It is not until their design gets more complicated that it all falls apart. Absolute position provides more flexibility but will sometimes diminish your control, use wisely.</p>
<h3>Test in multiple browsers</h3>
<p>There are various browsers on the web. It sometimes depends on who your website caters to but mind as well cover the major users. Test on desktop machines with Internet Explorer 6, 7, 8, 9, Chrome, Firefox, Safari. On mobile devices with Android, Opera Mini, iOS. Just make sure everything&#039;s consistent. This is a good chance to test how good your skills are too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crankberryblog.com/2010/the-ultimate-newbs-guide-to-learning-web-development-pt-1-the-basic/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Random row from your MySQL table with PHP, alternative to ORDER BY rand()</title>
		<link>http://www.crankberryblog.com/2010/random-row-from-your-mysql-table-with-php-alternative-to-order-by-rand</link>
		<comments>http://www.crankberryblog.com/2010/random-row-from-your-mysql-table-with-php-alternative-to-order-by-rand#comments</comments>
		<pubDate>Thu, 22 Apr 2010 21:28:04 +0000</pubDate>
		<dc:creator>Jerry Low</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Script]]></category>
		<category><![CDATA[Random]]></category>

		<guid isPermaLink="false">http://www.crankberryblog.com/?p=379</guid>
		<description><![CDATA[Working with MySQL and PHP is awesome. Its one of the best way to manage your data, but once in a while you may want to do some randomizing. One problem I came across recently was how to select a random row from your MySQL table with PHP. Using the ORDER BY rand() method in your query maybe a bit slow so here's what we've got.]]></description>
			<content:encoded><![CDATA[<p>Working with MySQL and PHP is awesome. Its one of the best way to manage your data, but once in a while you may want to do some randomizing. One problem I came across recently was how to select a random row from your MySQL table with PHP. Using the ORDER BY rand() method in your query maybe a bit slow so here&#039;s what we&#039;ve got.</p>
<h3>The Premade Method</h3>
<p>As I mentioned in the beginning. MySQL actually has a premade method that allows you to select a random row by querying for it.</p>
<div class="dean_ch" style="white-space: wrap;"><a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span><span class="st0">&quot;SELECT * FROM table ORDER BY rand() LIMIT 0,1&quot;</span><span class="br0">&#41;</span>;</div>
<p>This method is great but if you are randomizing from a large table this method is not very efficient and with a high volume of queries it really puts a strain on your DB.</p>
<h3>The New Method &#8211; Randomizing 1 Entry</h3>
<p>Now here&#039;s the new solution by selecting a random row with the offset. This method will query exactly to the random entry; thus, making this process much faster.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="re0">$query</span> = <span class="st0">&quot;SELECT * FROM table&quot;</span><br />
<span class="re0">$num_rows</span> = <a href="http://www.php.net/mysql_num_rows"><span class="kw3">mysql_num_rows</span></a><span class="br0">&#40;</span> <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span> <span class="re0">$query</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span>;</p>
<p><span class="co1">//Random Number</span><br />
<span class="re0">$rand_row</span> = <a href="http://www.php.net/rand"><span class="kw3">rand</span></a><span class="br0">&#40;</span><span class="nu0">0</span>, <span class="re0">$num_rows</span> &#8211; <span class="nu0">1</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">//Selecting the Random Row</span><br />
<span class="re0">$rand_row</span> = <span class="re0">$query</span> .= <span class="st0">&quot; LIMIT $rand_row, 1&quot;</span>;</div>
<p>Now you can modify the $query line to include your filters and sorting stuff, whatever you need.</p>
<h3>The New Method &#8211; Randomizing More than 1 Entry</h3>
<p>The above method right now can only select a single row. What if you want to randomized more than one item. Here&#039;s a method to select multiple items without overlap.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="co1">//Set the Amount of Random Rows you want</span><br />
<span class="re0">$rand_amount</span> = <span class="nu0">5</span>;</p>
<p><span class="re0">$query</span> = <span class="st0">&quot;SELECT * FROM table&quot;</span><br />
<span class="re0">$num_rows</span> = <a href="http://www.php.net/mysql_num_rows"><span class="kw3">mysql_num_rows</span></a><span class="br0">&#40;</span> <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span> <span class="re0">$query</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span>;</p>
<p><span class="co1">//Make sure you have more than the amount of random count you want</span><br />
<span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$num_rows</span> &lt;= <span class="re0">$rand_amount</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//An Array of Numbers Choosen</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$rand_array</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Do IT!!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="re0">$i</span> = <span class="nu0">1</span>; <span class="re0">$i</span> &gt;= <span class="re0">$rand_amount</span>; <span class="re0">$i</span>++ <span class="br0">&#41;</span> <span class="br0">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">do</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$rand_added</span> = <span class="kw2">FALSE</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$rand_num</span> = <a href="http://www.php.net/rand"><span class="kw3">rand</span></a><span class="br0">&#40;</span><span class="nu0">0</span>, <span class="re0">$num_rows</span> &#8211; <span class="nu0">1</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Should We Add to Array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!<a href="http://www.php.net/in_array"><span class="kw3">in_array</span></a><span class="br0">&#40;</span><span class="re0">$rand_num</span>, <span class="re0">$rand_array</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$rand_array</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="re0">$rand_num</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$rand_added</span> = <span class="kw2">TRUE</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">while</span> <span class="br0">&#40;</span>!<span class="re0">$rand_added</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//You now have a set of random row numbers to choose from</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Display The Result of What you have</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">foreach</span> <span class="br0">&#40;</span><span class="re0">$rand_array</span> <span class="kw1">as</span> <span class="re0">$rand_num</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_query</span> = <span class="re0">$query</span> . <span class="st0">&quot; LIMIT $rand_num, 1&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Displaying What you have</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$current_row</span> = <a href="http://www.php.net/mysql_fetch_array"><span class="kw3">mysql_fetch_array</span></a><span class="br0">&#40;</span> <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span> <span class="re0">$new_query</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>To be honest you can use the multiple randomizer and just set the random amount to 1 but the other method saves you like 20 lines of code. Imagine how much faster that could be!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crankberryblog.com/2010/random-row-from-your-mysql-table-with-php-alternative-to-order-by-rand/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

