Basic query using ColdFusion or PHP

August 18th, 2008

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.

Entry Filed under: coldfusion, php

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed

Calendar

October 2017
M T W T F S S
« Feb    
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Categories

Most Recent Posts

Feeds