Variables
Coldfusion variables are set with <cfset> tag. In PHP, a variable is set inside a <?php ...?> block of code with a "$" char before the var name. For example:
Coldfusion: <cfset myVar = 0>
PHP: <?php $myVar = 0?>
PHP: <?php $myVar = 0?>
Differences between URL variables:
Coldfusion: #URL.myVar#
PHP: $_GET['myVar'];
PHP: $_GET['myVar'];
Differences between POST variables:
Coldfusion: #POST.myVar#
PHP: $_POST['myVar'];
PHP: $_POST['myVar'];
Execute a query
Coldfusion queries are "beautiful" :) and very simple to define with <cfquery> tag. You have only to specify a datasource name (database you use in your application) and a query name:
<cfquery datasource="myDatasource" name="getUser">
SELECT * FROM USER
</cfquery>
SELECT * FROM USER
</cfquery>
If you use PHP, when you add a query, you have to include in your page all the parameters to connect to database. I suggest to read this post to have more info about this topic. A tipical query in PHP is structured in this way:
<?php
// Connection's Parameters
$db_host="localhost";
$db_name="database_name";
$username="database_username";
$password="database_password";
$db_con=mysql_connect($db_host,$username,$password);
$connection_string=mysql_select_db($db_name);
// Connection
mysql_connect($db_host,$username,$password);
mysql_select_db($db_name);
// Query
$sql = 'SELECT * FROM USER';
$getUser = mysql_query($sql);
?>
// Connection's Parameters
$db_host="localhost";
$db_name="database_name";
$username="database_username";
$password="database_password";
$db_con=mysql_connect($db_host,$username,$password);
$connection_string=mysql_select_db($db_name);
// Connection
mysql_connect($db_host,$username,$password);
mysql_select_db($db_name);
// Query
$sql = 'SELECT * FROM USER';
$getUser = mysql_query($sql);
?>
How you can see, I defined a variable $sql (with the query SQL code). mysql_query($sql) execute the query.
Query results
After the execution of a query you would show the query results. With coldfusion you ca use <cfouptut> tag and add the "query" parameter to specify what query's result you want to display.
<cfoutput query="getUser">
#name#, #email#, #city#
</cfoutput>
#name#, #email#, #city#
</cfoutput>
You can also use this dotted code if inside a <cfoutput> code you want to display results from different queries:
<cfoutput>
#getUser.name#, #getUser.email#, #getCity.city#
</cfoutput>
#getUser.name#, #getUser.email#, #getCity.city#
</cfoutput>
With PHP you have use mysql_fetch_array() method inside a while statement:
<?php
while ($row = mysql_fetch_array($getUser)){
echo $row['name'] . ',' ;
echo $row['email'] . ',' ;
echo $row['city'] . ',' ;
}
?>
while ($row = mysql_fetch_array($getUser)){
echo $row['name'] . ',' ;
echo $row['email'] . ',' ;
echo $row['city'] . ',' ;
}
?>
I hope this post about this topic can be useful to all PHP/Coldfusion beginner developers. I will add other infos in the next posts.
No comments:
Post a Comment