<?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>Bright Cherry &#187; MySQL &amp; PHP</title>
	<atom:link href="http://www.brightcherry.co.uk/scribbles/category/mysql-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.brightcherry.co.uk/scribbles</link>
	<description></description>
	<lastBuildDate>Tue, 20 Dec 2011 19:54:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>MySQL / PHP &#8211; Optimize MySQL Tables Script</title>
		<link>http://www.brightcherry.co.uk/scribbles/mysql-php-optimize-mysql-tables-script/</link>
		<comments>http://www.brightcherry.co.uk/scribbles/mysql-php-optimize-mysql-tables-script/#comments</comments>
		<pubDate>Fri, 22 Apr 2011 09:44:25 +0000</pubDate>
		<dc:creator>adm1n</dc:creator>
				<category><![CDATA[MySQL & PHP]]></category>

		<guid isPermaLink="false">http://www.brightcherry.co.uk/scribbles/?p=372</guid>
		<description><![CDATA[The MySQL Optimize Table command will effectively de-fragment a mysql table; it&#8217;s useful for tables which are frequently updated and/or rows are deleted. Optimizing will will help with overall performance. I&#8217;ve quickly written a PHP script that optimizes all MySQL tables in a chosen Database. All you need to do is fill in the correct [...]]]></description>
			<content:encoded><![CDATA[<p>The MySQL Optimize Table command will effectively de-fragment a mysql table; it&#8217;s useful for tables which are frequently updated and/or rows are deleted. Optimizing will will help with overall performance.</p>
<p>I&#8217;ve quickly written a PHP script that optimizes all MySQL tables in a chosen Database. All you need to do is fill in the correct DB settings, run the PHP script, and all the tables will be optimized. The script will also return the results in the following format:</p>
<p><img src="http://www.brightcherry.co.uk/images/blogimages/sql_optimize.jpg" height="301" width="520" title="Optimize MySQL Tables Script" alt="Optimize MySQL Tables Script" /></p>
<h4>PHP/MySQL code</h4>

<div class="wp_codebox"><table><tr id="p3723"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
</pre></td><td class="code" id="p372code3"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//db connection details</span>
<span style="color: #000088;">$host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;wordpress&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//connect to db</span>
<span style="color: #000088;">$db_connection</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$username</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not connect to db&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_connection</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not connect to table&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//get statuses for tables in db</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SHOW TABLE STATUS&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span>	<span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//initialize array</span>
<span style="color: #000088;">$tables</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// return the size in Kilobytes</span>
    <span style="color: #000088;">$table_size</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span> <span style="color: #0000ff;">&quot;Data_length&quot;</span> <span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span> <span style="color: #0000ff;">&quot;Index_length&quot;</span> <span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">1024</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$tables</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%.2f</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table_size</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//get total size of all tables</span>
    <span style="color: #000088;">$total_size</span> <span style="color: #339933;">+=</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$table_size</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// optimize tables</span>
    <span style="color: #000088;">$optimise_sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;OPTIMIZE TABLE <span style="color: #006699; font-weight: bold;">{$row['Name']}</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$optimise_result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$optimise_sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//get statuses for tables in db after optimization</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SHOW TABLE STATUS&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//initialize array</span>
<span style="color: #000088;">$optimised_tables</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span>	<span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// return the size in Kilobytes</span>
	<span style="color: #000088;">$table_size</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span> <span style="color: #0000ff;">&quot;Data_length&quot;</span> <span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span> <span style="color: #0000ff;">&quot;Index_length&quot;</span> <span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">1024</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$optimised_tables</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%.2f</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table_size</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//get total size of all tables after optimization</span>
	<span style="color: #000088;">$optimise_total_size</span> <span style="color: #339933;">+=</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$table_size</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h4>Code to output results</h4>

<div class="wp_codebox"><table><tr id="p3724"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code" id="p372code4"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>table width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;600&quot;</span> border<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>thead<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Table<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Size <span style="color: #009900;">&#40;</span>KB<span style="color: #009900;">&#41;</span><span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Optimised Size <span style="color: #009900;">&#40;</span>KB<span style="color: #009900;">&#41;</span><span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Optimised<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;/</span>thead<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>tbody<span style="color: #339933;">&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;?</span><span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tables</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$table</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$size</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
	<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$table</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$size</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$optimised_tables</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$table</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;</span>
			<span style="color: #000000; font-weight: bold;">&lt;?</span><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$optimised_tables</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$table</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
				<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$size</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$optimised_tables</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$table</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
			<span style="color: #000000; font-weight: bold;">&lt;?</span><span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
		<span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;?</span><span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
	<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>b<span style="color: #339933;">&gt;</span>Total<span style="color: #339933;">&lt;/</span>b<span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>b<span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$total_size</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>b<span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>b<span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$optimise_total_size</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>b<span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>b<span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$total_size</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$optimise_total_size</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>b<span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;/</span>tbody<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>table<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<h4>Download</h4>
<p><a href="http://www.brightcherry.co.uk/downloads/mysql-php-optimize-tables.zip" rel="nofollow" target="new" title="Download MySQL / PHP - Optimize MySQL Tables Script">Download MySQL / PHP &#8211; Optimize MySQL Tables Script</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.brightcherry.co.uk/scribbles/mysql-php-optimize-mysql-tables-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP / MySQL &#8211; Strip Content And Only Display Images From A MySQL Table</title>
		<link>http://www.brightcherry.co.uk/scribbles/php-mysql-strip-content-and-only-display-images-from-a-mysql-table/</link>
		<comments>http://www.brightcherry.co.uk/scribbles/php-mysql-strip-content-and-only-display-images-from-a-mysql-table/#comments</comments>
		<pubDate>Sun, 20 Jun 2010 20:56:10 +0000</pubDate>
		<dc:creator>adm1n</dc:creator>
				<category><![CDATA[MySQL & PHP]]></category>

		<guid isPermaLink="false">http://www.brightcherry.co.uk/scribbles/?p=231</guid>
		<description><![CDATA[This is another one of those quite specific pieces of code, so I doubt many people will need it. I think the code is quite useful, so I thought i&#8217;d make a post about it. Anyways, I&#8217;m going to show you how you can pull just images out of a field in a MySQL table. [...]]]></description>
			<content:encoded><![CDATA[<p>This is another one of those quite specific pieces of code, so I doubt many people will need it. I think the code is quite useful, so I thought i&#8217;d make a post about it. Anyways, I&#8217;m going to show you how you can pull just images out of a field in a MySQL table. I used this code yesterday, to specifically pull out all images from a WordPress Blog &#8211; to look at all the images that have been posted in ALL articles.</p>
<h5>What the following PHP/MySQL code will specifcally deliver&#8230;</h5>
<ul>
<li>It will pull out ONLY images</li>
<li>If one field has multiple images, it will display ALL of them</li>
</ul>

<div class="wp_codebox"><table><tr id="p2317"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p231code7"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//this query will only get the rows that have images in them</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span>
<span style="color: #0000ff;">&quot;SELECT [field]
FROM [table]
WHERE [field] LIKE '%&lt;img src=&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//execute the query</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//loop the results</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
 <span style="color: #666666; font-style: italic;">//this is the magic - preg_match matches all the images saves them into an array called images[]</span>
 <span style="color: #990000;">preg_match_all</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/&lt;img[^&gt;]+&gt;/i'</span><span style="color: #339933;">,</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ENTER FIELD'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$images</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//echo out all the images</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$images</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>That&#8217;s it! The print_r will show you all the images when you load the page. The preg_match_all line can actually be used with any string to pull out image references (or whatever regulat expression you require). I&#8217;m just specifcially demonstrating how I used it with WordPress/images.</p>
<p>If you don&#8217;t want to see the images, and just want to see the actual image URL&#8217;s, you can do the following&#8230;</p>

<div class="wp_codebox"><table><tr id="p2318"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p231code8"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//put the images[] array into a foreach loop</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$images</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$image</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//because each entry may have multiple images, we have to loop through each row to get each image</span>
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$img</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//remove the &lt;img&gt; tag</span>
    <span style="color: #000088;">$img</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;img src=&quot;'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">//explode each image by double quotes (or single quotes depending on how you quote your images), so we know where the image URL ends!</span>
    <span style="color: #000088;">$img</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&quot; '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">//rename the image URL to $file for ease</span>
    <span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$img</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
   <span style="color: #666666; font-style: italic;">//echo out each image URL</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$file</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Please let mw know if anyone actually uses this code and how useful it was&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brightcherry.co.uk/scribbles/php-mysql-strip-content-and-only-display-images-from-a-mysql-table/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP/MySQL- Escape All POST Or GET Variables For MySQL INSERT</title>
		<link>http://www.brightcherry.co.uk/scribbles/phpmysql-make-all-post-or-get-variables-safe-for-mysql-insert/</link>
		<comments>http://www.brightcherry.co.uk/scribbles/phpmysql-make-all-post-or-get-variables-safe-for-mysql-insert/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 19:26:58 +0000</pubDate>
		<dc:creator>adm1n</dc:creator>
				<category><![CDATA[MySQL & PHP]]></category>

		<guid isPermaLink="false">http://www.brightcherry.co.uk/scribbles/?p=199</guid>
		<description><![CDATA[I&#8217;m quickly going to demonstrate how to escape special characters in a string for all POST/GET variables safe for a MySQL INSERT query when passing values from a HTML form. So, let&#8217;s say we have a form like this on index.php: Details First Name: Surname: Email: 1 2 3 4 5 6 7 8 9 [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m quickly going to demonstrate how to escape special characters in a string for all POST/GET variables safe for a MySQL INSERT query when passing values from a HTML form.</p>
<p>So, let&#8217;s say we have a form like this on <b class="pink">index.php</b>:</p>
<form action="insert.php" method="POST">
<fieldset>
<legend>Details<br />
<legend>
<div>
<label>First Name:</label>
</div>
<div>
<input type="input" name="first_name" value="Bright" disabled="disabled" />
</div>
<div>
<label>Surname:</label>
</div>
<div>
<input type="input" name="surname" value="Cherry" disabled="disabled" />
</div>
<div>
<label>Email:</label>
</div>
<div>
<input type="input" name="email" value="design@brightcherry.co.uk" disabled="disabled" />
</div>
<div style="margin-top:10px;">
<input disabled="disabled" type="submit" value="Submit" />
</div>
</fieldset>
</form>

<div class="wp_codebox"><table><tr id="p19913"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code" id="p199code13"><pre class="html" style="font-family:monospace;">&lt;form action=&quot;insert.php&quot; method=&quot;POST&quot;&gt;
&lt;fieldset&gt;
&lt;legend&gt;Details&lt;legend&gt;
&lt;div&gt;
&lt;label&gt;First Name:&lt;/label&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;input type=&quot;input&quot; name=&quot;first_name&quot; value=&quot;Bright&quot; disabled=&quot;disabled&quot; /&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;label&gt;Surname:&lt;/label&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;input type=&quot;input&quot; name=&quot;surname&quot; value=&quot;Cherry&quot; disabled=&quot;disabled&quot; /&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;label&gt;Email:&lt;/label&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;input type=&quot;input&quot; name=&quot;email&quot; value=&quot;design@brightcherry.co.uk&quot; disabled=&quot;disabled&quot; /&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-top:10px;&quot;&gt;
&lt;input disabled=&quot;disabled&quot; type=&quot;submit&quot; value=&quot;Submit&quot; /&gt;
&lt;/div&gt;
&lt;/fieldset&gt;
&lt;/form&gt;</pre></td></tr></table></div>

<p>The form&#8217;s action is <b class="pink">insert.php</b>, so whichever method you choose (GET/POST) will get passed to insert.php. Now, before you INSERT the data into a MySQL table, you should ensure the data is safe to INSERT.</p>
<p>The PHP function you should use is <a href="http://php.net/manual/en/function.mysql-real-escape-string.php" title="PHP function: mysql_real_escape_string" rel="nofollow">mysql_real_escape_string</a>. </p>
<p>You can do the following before inserting the data (assuming the method type is POST):</p>

<div class="wp_codebox"><table><tr id="p19914"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p199code14"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'first_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'first_name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'surname'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'surname'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>But that&#8217;s not a very efficient method because you&#8217;re repeating the same function over and over, and if the form you&#8217;re using has a lot more fields, it will quickly become very problematic for a number of reasons.</p>
<p>Here&#8217;s a better and more efficient way of cleaning the data:</p>

<div class="wp_codebox"><table><tr id="p19915"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p199code15"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_keys</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$clean</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So what&#8217;s happening now is that ALL the POST variables are being cleaned in the <b class="pink">foreach</b> loop. You&#8217;re also renaming the POST values to $clean, but keeping the actual key value the same &#8211; so to echo the values you simply do this:</p>

<div class="wp_codebox"><table><tr id="p19916"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p199code16"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$clean</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'first_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$clean</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'surname'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$clean</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>It&#8217;s as easy as that <img src='http://www.brightcherry.co.uk/scribbles/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.brightcherry.co.uk/scribbles/phpmysql-make-all-post-or-get-variables-safe-for-mysql-insert/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MySQL &amp; PHP- Show Tables In Database</title>
		<link>http://www.brightcherry.co.uk/scribbles/mysql-php-show-tables-in-database/</link>
		<comments>http://www.brightcherry.co.uk/scribbles/mysql-php-show-tables-in-database/#comments</comments>
		<pubDate>Fri, 14 Nov 2008 09:01:00 +0000</pubDate>
		<dc:creator>adm1n</dc:creator>
				<category><![CDATA[MySQL & PHP]]></category>

		<guid isPermaLink="false">http://www.brightcherry.co.uk/scribbles/?p=94</guid>
		<description><![CDATA[Someone asked me the other day how to display the existing tables in their database with a PHP script. The query itself is pretty simple, but people seem to get confused about the PHP side of it. Then again, once you look at the code, you might be surprised how easy it really is&#8230; Printing [...]]]></description>
			<content:encoded><![CDATA[<p>Someone asked me the other day how to display the existing tables in their database with a PHP script. The query itself is pretty simple, but people seem to get confused about the PHP side of it. Then again, once you look at the code, you might be surprised how easy it really is&#8230;</p>
<h4>Printing tables with PHP</h4>

<div class="wp_codebox"><table><tr id="p9419"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p94code19"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #000088;">$showtablequery</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;
	SHOW TABLES
	FROM
	[database]
	&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$showtablequery_result</span>	<span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$showtablequery</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$showtablerow</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$showtablequery_result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$showtablerow</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>That query will show ALL tables, but you can use a LIKE function in the query as well, to get particular tables only.</p>
<h4>Printing particular tables with PHP</h4>

<div class="wp_codebox"><table><tr id="p9420"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p94code20"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #000088;">$showtablequery</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;
	SHOW TABLES
	FROM
	[database]
	LIKE
	'<span style="color: #009933; font-weight: bold;">%b</span>rightcherry%'
	&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$showtablequery_result</span>	<span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$showtablequery</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$showtablerow</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$showtablequery_result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$showtablerow</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>That query will pull out all tables with the word &#8216;brightcherry&#8217; in.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brightcherry.co.uk/scribbles/mysql-php-show-tables-in-database/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

