<?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>David Pratt &#187; php</title> <atom:link href="http://daipratt.co.uk/tag/php/feed/" rel="self" type="application/rss+xml" /><link>http://daipratt.co.uk</link> <description>Concerned about Website Construction &#38; SEO</description> <lastBuildDate>Thu, 17 May 2012 19:14:00 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <item><title>Exporting data from SQL Server Express 2005 to CSV using PHP</title><link>http://daipratt.co.uk/exporting-data-sql-server-2005-express-csv-php-mssql/</link> <comments>http://daipratt.co.uk/exporting-data-sql-server-2005-express-csv-php-mssql/#comments</comments> <pubDate>Fri, 10 Jun 2011 08:55:28 +0000</pubDate> <dc:creator>David Pratt</dc:creator> <category><![CDATA[Tech]]></category> <category><![CDATA[db]]></category> <category><![CDATA[php]]></category> <guid
isPermaLink="false">http://daipratt.co.uk/?p=1586</guid> <description><![CDATA[A tutorial that explains how to connect to a SQL Server 2005 Express database from a PHP script, and then output a CSV file of a table within that database.]]></description> <content:encoded><![CDATA[<p>Annoyingly, one of the limitations of SQL Server Express 2005 is that it doesn&#8217;t allow you to export data as CSV, or any other format (bar SQL) for that matter. This is fine is you have a small dataset as you can just copy the data out of the results pane, and then paste into a spreadsheet program to save in the format of your choosing. If like me on the other hand whereby you have a massive result set of +100k records then that doesn&#8217;t quite work.</p><p>Googleing around for a solution I came across a couple of different options (using DTS Wizard, another using a 3rd party app etc.), but nothing looked like it could do the job I was after &#8211; basically I wanted a no-fuss phpmyadmin style export to csv option. Dissatisfied with the options out there, I ended up deciding to write a php script that would connect to the db using the mssql php extension, get the required data, and automatically spit out a csv file. Writing the script was the easy part, the difficult bit was getting PHP to talk to the database due to an incompatibility between the SQL Server Express 2005 db driver and the php extension. To get PHP to play nicely with MS SQL Server 2005 Express you need to resolve this driver problem, here&#8217;s how you do it:</p><ol><li>Search your computer for ntwdblib.dll</li><li>If you right click on each occurance of the file (I had one in the php, apache and system32 directories)</li><li>Hit the details tab, and if any of them say a number that ISN&#8217;T <strong>2000.80.194.0</strong> then you need to <a
href="http://www.google.co.uk/search?q=2000.80.194.0+ntwdblib.dll+download">download that version</a> of the file.</li><li>Once you have downloaded the file, you need to replace all of the existing ntwdblib.dll files with the one you have downloaded, or figure out which one your php version is using. Obviously, make back-ups first.</li></ol><p>A more in depth tutorial on how to do this can be found<br
/> <href="http://dba.fyicenter.com/faq/sql_server_2/ntwdblib_dll_SQL_Server_Client_Libarary_DLL.html">here</a>.</p><p>Here&#8217;s the code for connecting to the db, generating a CSV file and outputting to file in the simplest way I could come up with:</p><pre class="brush: php">
//MSSQL connection string details.
$dbhost = "LOCALHOST\SQLEXPRESS";
$dbuser = 'dave';
$dbpass = 'password';
$dbname = 'target_db_name';
$db = mssql_connect($dbhost, $dbuser, $dbpass);
if (!$db) {
	die('Could not connect to mssql - check your connection details &#038; make sure that ntwdblib.dll is the right version!');
}
$db_selected = mssql_select_db($dbname, $db);
if (!$db_selected) {
	die('Could not select mssql db');
}
$sql = "SELECT * FROM target_database_table_name";
$results = mssql_query($sql, $db);
//Generate CSV file - Set as MSSQL_ASSOC as you don't need the numeric values.
while ($l = mssql_fetch_array($results, MSSQL_ASSOC)) {
	foreach($l AS $key => $value){
		//If the character " exists, then escape it, otherwise the csv file will be invalid.
		$pos = strpos($value, '"');
		if ($pos !== false) {
			$value = str_replace('"', '\"', $value);
		}
		$out .= '"'.$value.'",';
	}
	$out .= "\n";
}
mssql_free_result($results);
mssql_close($db);
// Output to browser with the CSV mime type
header("Content-type: text/x-csv");
header("Content-Disposition: attachment; filename=table_dump.csv");
echo $out;
</pre><p><i>For reference, I&#8217;m using PHP 5.2.3 on wamp &#8211; the driver fix mentioned above didn&#8217;t seem to work with 5.3.*</i></p> ]]></content:encoded> <wfw:commentRss>http://daipratt.co.uk/exporting-data-sql-server-2005-express-csv-php-mssql/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Create photo albums and upload photos using the Facebook Graph API</title><link>http://daipratt.co.uk/facebook-api-upload-photo/</link> <comments>http://daipratt.co.uk/facebook-api-upload-photo/#comments</comments> <pubDate>Sat, 11 Sep 2010 16:08:48 +0000</pubDate> <dc:creator>David Pratt</dc:creator> <category><![CDATA[Tech]]></category> <category><![CDATA[facebook]]></category> <category><![CDATA[php]]></category> <guid
isPermaLink="false">http://daipratt.co.uk/?p=1295</guid> <description><![CDATA[An example using the Facebook Graph API that shows how to create a photo album and insert photos into it. The example uses PHP.]]></description> <content:encoded><![CDATA[<p>UPDATE: Thanks to some of the comments below I have been able to revise the code &#8211; thanks Luke &#038; Guilherme</p><p>I thought I&#8217;d share some recent learning&#8217;s with using the <a
href="http://developers.facebook.com/docs/api">Facebook Graph API</a> as there seems to be so few examples out there about how to utilise various aspects of its functionality.  I will explain how to create a photo album and then insert a photo into it using PHP.</p><p>The code example assumes that you have already generated an authenticated session and have the correct permissions (read_stream, publish_stream, photo_upload, user_photos, user_photo_video_tags). If you haven&#8217;t already done so, then there is a <a
href="http://thinkdiff.net/facebook/graph-api-iframe-base-facebook-application-development/">good example here</a> about how to do so and you can read more about the Facebook permissions on the <a
href="http://developers.facebook.com/docs/authentication/permissions">Facebook dev site</a>.</p><p>The following snippet creates a photo album and then uploads a photo into it:</p><pre class="brush: php">
//At the time of writing it is necessary to enable upload support in the Facebook SDK, you do this with the line:
$facebook->setFileUploadSupport(true);
&nbsp;
//Create an album
$album_details = array(
		'message'=> 'Album desc',
		'name'=> 'Album name'
);
$create_album = $facebook->api('/me/albums', 'post', $album_details);
&nbsp;
//Get album ID of the album you've just created
$album_uid = $create_album['id'];
&nbsp;
//Upload a photo to album of ID...
$photo_details = array(
	'message'=> 'Photo message'
);
$file='app.jpg'; //Example image file
$photo_details['image'] = '@' . realpath($file);
&nbsp;
$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);
</pre><p>You can add more detail to the created album and photo than I have in the example by using <a
href="http://developers.facebook.com/docs/reference/api/post">additional parameters</a> within the arguments array.</p> ]]></content:encoded> <wfw:commentRss>http://daipratt.co.uk/facebook-api-upload-photo/feed/</wfw:commentRss> <slash:comments>63</slash:comments> </item> <item><title>Best way to store IP addresses in MySQL</title><link>http://daipratt.co.uk/mysql-store-ip-address/</link> <comments>http://daipratt.co.uk/mysql-store-ip-address/#comments</comments> <pubDate>Tue, 10 Aug 2010 08:53:35 +0000</pubDate> <dc:creator>David Pratt</dc:creator> <category><![CDATA[Tech]]></category> <category><![CDATA[mysql]]></category> <category><![CDATA[php]]></category> <guid
isPermaLink="false">http://daipratt.co.uk/?p=1258</guid> <description><![CDATA[It's tempting to store IP addresses in a database as a varchar of length 15 in the absence of a dedicated IPv4 field type in mysql, but that isn't the most efficient way of doing so...]]></description> <content:encoded><![CDATA[<p>It&#8217;s tempting to store IP addresses in a database as a VARCHAR(15) in the absence of a dedicated IP address field type in mysql, but that isn&#8217;t the most efficient way of doing so. The best way that I&#8217;ve come across to store an IPv4 address is to store it as an unsigned integer.  In phpmyadmin you might set up an ip address field so that it looks something like:<br
/><div
id="attachment_1264" class="wp-caption alignnone" style="width: 363px"><a
href="http://daipratt.co.uk/wp-content/uploads/2010/08/phpmyadmin-ip-address-setup.png"><img
src="http://daipratt.co.uk/wp-content/uploads/2010/08/phpmyadmin-ip-address-setup.png" alt="Screenshot of the PHPMyAdmin field setup for an IP address field" title="PHPMyAdmin field setup for IP address field" width="353" height="243" class="size-full wp-image-1264" /></a><p
class="wp-caption-text">Screenshot of the PHPMyAdmin field setup for an IP address field</p></div></p><p>You&#8217;ll realise I&#8217;m sure that you can&#8217;t just add the dotted IP address straight into an INT field without first converting it into a valid format.  For that you&#8217;ll need a PHP function called <a
href="http://php.net/manual/en/function.ip2long.php">ip2long</a> which will convert a string containing an IP dotted address into a integer that can be stored in the INT field.</p><p>Here is a quick example of how you might go about getting the real IP address of a client and then storing and retrieving its value from the mysql DB:</p><pre class="brush: php">
//Test if it is a shared client
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
  $ip=$_SERVER['HTTP_CLIENT_IP'];
//Is it a proxy address
}elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
  $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
  $ip=$_SERVER['REMOTE_ADDR'];
}
//The value of $ip at this point would look something like: "192.0.34.166"
$ip = ip2long($ip);
//The $ip would now look something like: 1073732954
</pre><p>Now that you have the real IP address of the client converted to the INT format, you can write it into the DB as you normally would:</p><pre class="brush: php">
$sql = "INSERT INTO user(ip) VALUES('$ip')";
$dbQuery = mysql_query($sql,$dbLink);
</pre><p>To retrieve the original IP address from the database you can use the mysql function <a
href="http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_inet-ntoa">INET_NTOA</a> like so:</p><pre class="brush: sql">
SELECT INET_NTOA(ip) FROM 'user' WHERE 1
</pre><p>Alternately you could use the PHP function <a
href="http://www.php.net/manual/en/function.long2ip.php">longtoip</a> to convert the returned INT value into the dotted IPv4 address in the PHP code instead, and you could even add the dotted IP address to the INT field in the db using the mysql funtion INET_ATON.</p><p>Storing IP addresses in this manner is beneficial because it takes less space than storing it as a string.  The other benefit is that lookups are faster because integer comparisons are quicker than string comparisons.</p> ]]></content:encoded> <wfw:commentRss>http://daipratt.co.uk/mysql-store-ip-address/feed/</wfw:commentRss> <slash:comments>14</slash:comments> </item> <item><title>Non-standard page size&#8217;s in TCPDF</title><link>http://daipratt.co.uk/tcpdf-page-sizes/</link> <comments>http://daipratt.co.uk/tcpdf-page-sizes/#comments</comments> <pubDate>Wed, 23 Jun 2010 18:11:12 +0000</pubDate> <dc:creator>David Pratt</dc:creator> <category><![CDATA[Tech]]></category> <category><![CDATA[php]]></category> <category><![CDATA[tcpdf]]></category> <guid
isPermaLink="false">http://daipratt.co.uk/?p=1249</guid> <description><![CDATA[If you need to create a PDF using TCPDF that isn't a standard size (A4, A5, B1, LETTER etc.) then this snippet might help you out: $resolution= array(100, 100);  $pdf->AddPage('P', $resolution);]]></description> <content:encoded><![CDATA[<p>If you need to create a PDF using <a
href="http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf">TCPDF</a> that isn&#8217;t a standard size (A4, A5, B1, LETTER etc.) then it is possible to specify a custom size even though it isn&#8217;t that well documented. To do this you need to pass an array containing the desired width and the height of the PDF instead a formatted parameter such as &#8216;A4&#8242;.  For example:</p><pre class="brush: php">
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
&nbsp;
// add a page
$resolution= array(100, 100);
$pdf->AddPage('P', $resolution);
</pre><p>Make sure you express the height and width in the unit of measurement (pt, px, mm, cm etc.) that you are using as well.</p> ]]></content:encoded> <wfw:commentRss>http://daipratt.co.uk/tcpdf-page-sizes/feed/</wfw:commentRss> <slash:comments>10</slash:comments> </item> <item><title>Using Crontab with Plesk to call PHP files</title><link>http://daipratt.co.uk/crontab-plesk-php/</link> <comments>http://daipratt.co.uk/crontab-plesk-php/#comments</comments> <pubDate>Thu, 06 May 2010 18:39:01 +0000</pubDate> <dc:creator>David Pratt</dc:creator> <category><![CDATA[Tech]]></category> <category><![CDATA[crontab]]></category> <category><![CDATA[php]]></category> <category><![CDATA[plesk]]></category> <guid
isPermaLink="false">http://daipratt.co.uk/?p=1214</guid> <description><![CDATA[How to set up a scheduled job on Plesk that calls a PHP file at an interval of your choosing.]]></description> <content:encoded><![CDATA[<p>Having battled with getting a php file to be called from a scheduled task (a.k.a. a crontab task) using the Plesk interface panel on a Linux box, I thought I&#8217;d share a bit of a how-to on it because I struggled to find anything helpful out there on the topic.</p><p>Here is screen shot of a completed &#8220;Schedule New Task&#8221; form so that you can see what a working configuration looks like:<br
/><div
id="attachment_1219" class="wp-caption alignnone" style="width: 483px"><a
href="http://daipratt.co.uk/wp-content/uploads/2010/05/crontab.png"><img
src="http://daipratt.co.uk/wp-content/uploads/2010/05/crontab.png" alt="Setting up Crontab from within Plesk to load a php file." title="Crontab" width="473" height="389" class="size-full wp-image-1219" /></a><p
class="wp-caption-text">Setting up Crontab from within Plesk to load a php file.</p></div></p><p>The fields on the form look fairly self explanatory to fill out, but the first gotcha for me was how to get it so that the job runs every hour instead of on just one named hour. To get the cron to run every hour you just need to use an asterisk (*), the same technique can be applied for all other time frequencies as well.</p><p>Once you&#8217;ve set the cron to run at the required interval, you will need to tell it what command to call. This isn&#8217;t obvious either.  Firstly what you need to do is call php or state the path to the php folder, then pass it the parameter &#8220;-q&#8221; (to suppress the HTTP header output), and then state the full path of the php file that you want to run e.g.</p><pre class="brush: plain">
php -q httpdocs/cron.php
</pre><p>If you just put the path to the name of the php file that you want to run, then the cron will throw an error.</p><p><em>For reference, I&#8217;m using Plesk 9.5.1 &amp; PHP 5.3</em></p> ]]></content:encoded> <wfw:commentRss>http://daipratt.co.uk/crontab-plesk-php/feed/</wfw:commentRss> <slash:comments>31</slash:comments> </item> <item><title>How to set up a local server on Windows</title><link>http://daipratt.co.uk/how-to-set-up-a-local-server-on-windows/</link> <comments>http://daipratt.co.uk/how-to-set-up-a-local-server-on-windows/#comments</comments> <pubDate>Mon, 11 May 2009 18:46:33 +0000</pubDate> <dc:creator>David Pratt</dc:creator> <category><![CDATA[Tech]]></category> <category><![CDATA[apache]]></category> <category><![CDATA[mysql]]></category> <category><![CDATA[php]]></category> <category><![CDATA[server]]></category> <category><![CDATA[wordpress]]></category> <guid
isPermaLink="false">http://localhost/wordpress/?p=1</guid> <description><![CDATA[Instructions on how to setup a local server manually on a windows box; running Apache, MySQL and PHP.]]></description> <content:encoded><![CDATA[<p>Have you ever wondered how to set up a local server on your windows box running with Apache, MySQL and PHP?  I have. Here&#8217;s how I did it.</p><h3>Mission 1: Download everything</h3><p>Create a folder on your desktop and put the following downloads in it:</p><ul><li><a
href="http://httpd.apache.org/download.cgi">Apache</a> &#8211; <a
href="http://apache.mirror.anlx.net/httpd/binaries/win32/apache_2.2.11-win32-x86-no_ssl.msi">direct</a></li><li><a
href="http://dev.mysql.com/downloads/mysql/5.1.html">MySQL</a> &#8211; Get the windows installer</li><li><a
href="http://www.php.net/downloads.php">PHP</a> &#8211; <a
href="http://uk.php.net/get/php-5.2.8-win32-installer.msi/from/this/mirror">direct</a></li><li><a
href="http://www.libgd.org/Downloads">Module &#8211; GD</a> &#8211; <a
href="http://www.libgd.org/releases/gd-2.0.35.tar.gz">direct</a></li><li><a
href="#">Module &#8211; Mail Server</a> &#8211; <a
href="#">direct</a></li><li><a
href="http://www.phpmyadmin.net/home_page/downloads.php">PHPMyAdmin</a> &#8211; <a
href="http://prdownloads.sourceforge.net/phpmyadmin/phpMyAdmin-3.1.1-english.zip?download#!md5!64f77187056154e044bf9596728127a3">direct</a></li></ul><h3>Mission 2: Install everything with default settings</h3><p>Install the applications in the order that I have listed.</p><p>Apart from PHPMyAdmin. Park that.</p><p>Make sure that you write down with a pen and paper all of your paired usernames and passwords &#8211; you do not want to be in a situation where you end up sitting there wondering whether the default username was root, admin, guest and whether you paired it with your usual password, a special easy one or no password at all!</p><p>So, if you do cock up and can&#8217;t remember your username and password (as I did for MySQL) then you&#8217;ll need to uninstall it with a special tool as the windows one will keep you locked out on reinstall.  You&#8217;ll need <a
href="http://www.revouninstaller.com/revo_uninstaller_free_download.html">Revo univstaller</a> unless you fancying hitting F3 about 50 times in regedit.</p><h3>Mission 3: Configure Apache</h3><p>Unfortunately everything doesn&#8217;t work and play nice straight out of the box. It needs some love (as most things tech do).</p><h4>Enable Apache mod_rewrite module</h4><p>This is the feature that allows sexxy clean urls (permalinks in WordPress) with none of this .php?foo=blah rubbish.</p><ol><li>Open your httpd.conf configuration file (C:Program FilesApache Software FoundationApache2.2confhttpd.conf) with Notepad.</li><li>Remove the # on the #LoadModule rewrite_module modules/mod_rewrite.so line so it becomes LoadModule rewrite_module modules/mod_rewrite.so only.</li><li>Save httpd.conf .</li></ol><h4>Enable AllowOverride for web directory folder (htdocs)</h4><p>This allows the rule set in .htaccess file to over-ride the main one. A bit like upping the specificity in CSS if thats your thing&#8230;</p><ol><li>Open your httpd.conf configuration file (C:Program FilesApache Software FoundationApache2.2confhttpd.conf) with Notepad.</li><li>Under the directory C:Program FilesApache Software FoundationApache2.2htdocs section, change the line that says &#8220;AllowOverride None&#8221; to &#8220;AllowOverride All&#8221;.</li><li>Save your httpd.conf .</li></ol><h3>Mission 4: Create and run info.php</h3><p>Create a file called <strong>info.php</strong>, place it in the directory <span>C:Program FilesApache Software FoundationApache2.2htdocs</span> and put the following code in it:</p><pre class="javascript">&lt;?php
phpinfo();
phpinfo(INFO_MODULES);
?&gt;</pre><p>Now fire up your web browser and type in the url <a
href="http://localhost/info.php">http://localhost/info.php</a> and the page should render with a load of information.  On it you should see that &#8220;rewrite module&#8221; is enabled and gd library is active, zend php is working.</p><h3>Mission 5: Get PHP to talk to MySQL</h3><p>Now that we know PHP works we can configure it to work with MySQL, but first we need to do a quick restart of Apache &#8211; double click on the Apache system tray icon and click restart.</p><p>Next it is time to find PHPMyAdmin.zip you have sitting in that desktop folder, unzip it to this location: C:Program FilesApache Software FoundationApache2.2htdocsphpmyadmin</p><p>Open your browser and navigate to <a
href="http://localhost/phpmyadmin/">http://localhost/phpmyadmin/</a></p><p>Enter all of the details that you wrote down on that piece of paper from earlier.</p><p>And do restart you local Apache web server. Now your can enable the pretty permalink structure from the WordPress dashboard and it will work nicely after that.</p><p>There you have it;  One local server that doesn&#8217;t do a lot. You could have just installed <a
href="http://www.apachefriends.org/en/xampp.html">this</a> if you want the easy option! I&#8217;ll do a follow up post about some of the things that you might want to do with your local server soon&#8230;</p> ]]></content:encoded> <wfw:commentRss>http://daipratt.co.uk/how-to-set-up-a-local-server-on-windows/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
