coldfusion

Basic query using ColdFusion or PHP

Following on from our previous post showing the process of removing spaces from strings with both ColdFusion and PHP, this example shows the process for querying some data and returning it to the browser as an unordered list. Again, a very basic example but one which shows how these two technologies handle the same thing.

so…ColdFusion.

in the ColdFusion Administrator set up your datasource – DATA & SERVICES > Data Sources then type in the name of your datasource(we’ll call it something original…so, “test” it is then!), select the correct Driver and press “Add”, then just type in the name of your DB, the server(probably localhost) and the username and password thats your DB available to ColdFusion on your server, all you need to remember while writing your app is the datasource name.

A sample Query looks like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!--- connect to your datasource and select what you want:--->
<cfquery name="listData" datasource="test">
    select dataOne, dataTwo, dataThree
    from data
</cfquery>

<!---and output it in an unordered list:--->
<cfoutput>
<ul>
    <li>#dataOne#</li>
    <li>#dataTwo#</li>
    <li>#dataThree#</li>
</ul>
</cfoutput>

job done!

and so over to PHP…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php

// first we need a connection to the DB
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("myDatabase") or die(mysql_error());

// retrieve the required data
$getData = mysql_query("SELECT dataOne, dataTwo, dataThree FROM data")
or die(mysql_error());

// and output into an unordered list
echo "<ul>";
while($row = mysql_fetch_array($getData))
{
    echo "<li>".$row['dataOne']."</li>";
    echo "<li>".$row['dataTwo']."</li>";
    echo "<li>".$row['dataThree']."</li>";
}
echo "</ul>";

?>

and there you have it, the same query returning the same data in both ColdFusion and PHP.

Add comment August 18th, 2008

Removing spaces from a string with ColdFusion or php

This is one of those little problems that pop up frequently and to show how it’s done in CFML as well as our other house language PHP, we’ve put these examples up for you. This method is using a regular expression.

To remove ALL the spaces from a string:

1
2
3
4
5
6
<!--- set up a test variable --->
<cfset mySpaceFilledString = " east coast interactive love coldfusion " />
<!--- remove ALL spaces from the string --->
<cfset mySpaceFreeVariable = ReReplace(mySpaceFilledString, "[[:space:]]","","ALL")>
<!--- check it works --->
<cfdump var = #mySpaceFreeVariable#>

The above returns “eastcoastinteractivelovecoldfusion” – all spaces have been removed

And the PHP version

1
2
3
4
5
6
7
8
<?php
// set up a test variable
$mySpaceFilledString = " east coast interactive love php ";
// remove all spaces from the string
$mySpaceFreeVariable = str_replace(" ", "", $mySpaceFilledString);
// check it works
echo $mySpaceFreeVariable;
?>

The above returns “eastcoastinteractivelovephp” – again, all spaces have been removed

To simply remove the spaces at the start and end of a string:

1
2
3
4
5
6
<!--- set up a test variable --->
<cfset mySpaceFilledString = " east coast interactive love coldfusion " />
<!--- remove spaces from start and end of the string --->
<cfset mySpaceFreeVariable = Trim(mySpaceFilledString)>
<!--- check it works --->
<cfdump var = #mySpaceFreeVariable#>

The above returns “east coast interactive love coldfusion” – spaces at the start and end have been removed only

And the PHP version

1
2
3
4
5
6
7
8
<?php
// set up a test variable
$mySpaceFilledString = " east coast interactive love php ";
// remove spaces from start and end of the string
$myTrimmedVariable = trim($mySpaceFilledString);
//check it works
echo $myTrimmedVariable;
?>

The above returns “east coast interactive love php” – again, all spaces have been removed from the start and end only.

Add comment August 12th, 2008

Coldfusion 8 Launches

The wait is over for the next major release of Coldfusion. Version 8 boasts some fantastic new features, integration with other technologies and some cool AJAX features, (we sneakily built a google suggest type application in about 30 minutes in our development environment for example). Go check it out here: http://www.adobe.com/products/coldfusion/

Add comment July 30th, 2007

Calendar

August 2017
M T W T F S S
« Feb    
 123456
78910111213
14151617181920
21222324252627
28293031  

Categories

Most Recent Posts

Feeds