Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,437 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,331 people online right now. Registration is fast and FREE... Join Now!




PHP within Coldfusion

2 Pages V  1 2 >  
Reply to this topicStart new topic

PHP within Coldfusion

jeffmc21
22 Aug, 2008 - 11:48 AM
Post #1

New D.I.C Head
*

Joined: 27 Nov, 2007
Posts: 47


My Contributions
I am curious about using <cfinclude> to include an external file that has a call to a database and the parsing of the results. I'm wanting to do the call and the parse in PHP, but the page that it will be included on is Coldfusion. Is that possible?
User is offlineProfile CardPM
+Quote Post

sansclue
RE: PHP Within Coldfusion
22 Aug, 2008 - 12:03 PM
Post #2

D.I.C Head
**

Joined: 21 Nov, 2007
Posts: 113



Thanked: 7 times
My Contributions
QUOTE(jeffmc21 @ 22 Aug, 2008 - 12:48 PM) *

I am curious about using <cfinclude> to include an external file that has a call to a database and the parsing of the results. I'm wanting to do the call and the parse in PHP, but the page that it will be included on is Coldfusion. Is that possible?


I don't think a cfinclude would work because it would not execute the php code, just display it. I don't use php, but my guess would be to try cfhttp instead. Then include the cfhttp results. If you are using CF8, another slightly more complicated option might be:

http://www.houseoffusion.com/groups/cf-tal...id:51958#280022
User is offlineProfile CardPM
+Quote Post

jeffmc21
RE: PHP Within Coldfusion
22 Aug, 2008 - 03:53 PM
Post #3

New D.I.C Head
*

Joined: 27 Nov, 2007
Posts: 47


My Contributions
I'm actually wanting to execute this connection, SQL call, and results display:

php

<?php

// Create the connection file with connection information

$host = "hostname";
$username = "username";
$password = "password";

// Connect to Database

$connection = @mysql_connect($host,$username,$password);
mysql_select_db(database_name,$connection);

$result = mysql_query("SELECT DATE_FORMAT(date,'%b %d, %Y') as article_day, `article_name`, `article_link`, `article_text` FROM `table_name` WHERE `date` <= CURDATE() ORDER BY event_date LIMIT 3");


while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<div class="cssarticle">
<div class="headline">'. $row["article_name"] .'</div>
<div class="articledate">'. $row["article_day"].'</div>
<div class="articleintro">'. $row["article_text"].'</div>
<div class="articlelink"><a href="'. $row["article_link"].'">Read More...</a></div>
</div>';
}

mysql_free_result($result);


?>


I'm extremely, extremely new to Coldfusion, so if someone could help show me how to do this, it'd be better than the initial request of PHP in CF.

Thanks so much!!
User is offlineProfile CardPM
+Quote Post

sansclue
RE: PHP Within Coldfusion
22 Aug, 2008 - 05:15 PM
Post #4

D.I.C Head
**

Joined: 21 Nov, 2007
Posts: 113



Thanked: 7 times
My Contributions
QUOTE(jeffmc21 @ 22 Aug, 2008 - 04:53 PM) *

I'm actually wanting to execute this connection, SQL call, and results display:

php

<?php

// Create the connection file with connection information

$host = "hostname";
$username = "username";
$password = "password";

// Connect to Database

$connection = @mysql_connect($host,$username,$password);
mysql_select_db(database_name,$connection);

$result = mysql_query("SELECT DATE_FORMAT(date,'%b %d, %Y') as article_day, `article_name`, `article_link`, `article_text` FROM `table_name` WHERE `date` <= CURDATE() ORDER BY event_date LIMIT 3");


while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<div class="cssarticle">
<div class="headline">'. $row["article_name"] .'</div>
<div class="articledate">'. $row["article_day"].'</div>
<div class="articleintro">'. $row["article_text"].'</div>
<div class="articlelink"><a href="'. $row["article_link"].'">Read More...</a></div>
</div>';
}

mysql_free_result($result);


?>


I'm extremely, extremely new to Coldfusion, so if someone could help show me how to do this, it'd be better than the initial request of PHP in CF.

Thanks so much!!



No problem. Yes, it should be easy to convert the php code to ColdFusion.

To run queries against a database you generally create a datasource in the ColdFusion Administrator. The datasource stores all of the settings (db name, username, etc). So whenever you want to run a query, you just supply the datasource name. Technically you could open a dynamic connection to the database, if you want. But it is really more work than is necessary. If you need help with the datasource settings, just ask. (Include your CF and mySQL version too)

To run queries, you use the <cfquery> tag. You just supply a "name" and "datasource", then put your sql statement inside the tags. ColdFusion then populates a variable with the query results.

CODE

// NOT TESTED
// Connect to Database
<cfquery name="getArticle" datasource="yourDatasourceNameHere">
SELECT DATE_FORMAT(date,'%b %d, %Y') as article_day, `article_name`, `article_link`, `article_text`
FROM    `table_name`
WHERE  `date` <= CURDATE()
ORDER BY event_date
LIMIT 3
</cfquery>


After you run the query, you can display the results using the <cfoutput> tag. Just provide the name you used for the query. CF will then iterate through the results. Inside the loop, the query columns are available as local variables. To output the value of a variable, you use the syntax: #variableName#. The # signs tell CF to evaluate what's inside.

So your while loop would be replaced with something like this:
CODE

<cfoutput query="getArticle">
   <div class="cssarticle">
      <div class="headline"> #article_name#</div>
      <div class="articledate">#article_day#</div>
      <div class="articleintro">#article_text#</div>
      <div class="articlelink"><a href="'#article_link#">Read More...</a></div>
   </div>
</cfoutput>


You might also want to check out livedocs (online documentation). The descriptions and examples are very helpful. It is the best reference for CF syntax. I know CF pretty well, but I use it all the time!
http://livedocs.adobe.com/coldfusion/7/htmldocs/00000316.htm

HTH

This post has been edited by sansclue: 22 Aug, 2008 - 05:17 PM
User is offlineProfile CardPM
+Quote Post

jeffmc21
RE: PHP Within Coldfusion
22 Aug, 2008 - 07:52 PM
Post #5

New D.I.C Head
*

Joined: 27 Nov, 2007
Posts: 47


My Contributions
That sounds, and look, exactly like what I'm wanting to accomplish. I'm operating a Cartweaver site and don't actually have CF installed on my machine, so I'm not sure about creating datasources, etc. How would I go about dynamically connecting to the database using CF?

User is offlineProfile CardPM
+Quote Post

sansclue
RE: PHP Within Coldfusion
22 Aug, 2008 - 09:10 PM
Post #6

D.I.C Head
**

Joined: 21 Nov, 2007
Posts: 113



Thanked: 7 times
My Contributions
QUOTE(jeffmc21 @ 22 Aug, 2008 - 08:52 PM) *

That sounds, and look, exactly like what I'm wanting to accomplish. I'm operating a Cartweaver site and don't actually have CF installed on my machine, so I'm not sure about creating datasources, etc. How would I go about dynamically connecting to the database using CF?


I would ask whomever is hosting the site to advise you on setting up a datasource. Datasources are really the way to go for a number of reasons, and you only have to do it once.

Personally I would not recommend using dynamic connections unless you know your way around CF and java and are aware of the pros and cons. Dynamic connections are more involved, so there is greater room for error. Plus they lack some of the performance enhancements of datasources, like connection pooling.

http://tutorial402.easycfm.com/

User is offlineProfile CardPM
+Quote Post

jeffmc21
RE: PHP Within Coldfusion
23 Aug, 2008 - 08:23 AM
Post #7

New D.I.C Head
*

Joined: 27 Nov, 2007
Posts: 47


My Contributions
QUOTE(sansclue @ 22 Aug, 2008 - 10:10 PM) *

QUOTE(jeffmc21 @ 22 Aug, 2008 - 08:52 PM) *

That sounds, and look, exactly like what I'm wanting to accomplish. I'm operating a Cartweaver site and don't actually have CF installed on my machine, so I'm not sure about creating datasources, etc. How would I go about dynamically connecting to the database using CF?


I would ask whomever is hosting the site to advise you on setting up a datasource. Datasources are really the way to go for a number of reasons, and you only have to do it once.

Personally I would not recommend using dynamic connections unless you know your way around CF and java and are aware of the pros and cons. Dynamic connections are more involved, so there is greater room for error. Plus they lack some of the performance enhancements of datasources, like connection pooling.

http://tutorial402.easycfm.com/


Does it matter at all that I'm connecting to a database that is hosted by a different company, under a different domain? So it would be a remote connection. Is a datasource still the best approach or does that matter?
User is offlineProfile CardPM
+Quote Post

sansclue
RE: PHP Within Coldfusion
23 Aug, 2008 - 08:46 AM
Post #8

D.I.C Head
**

Joined: 21 Nov, 2007
Posts: 113



Thanked: 7 times
My Contributions
QUOTE(jeffmc21 @ 23 Aug, 2008 - 09:23 AM) *

Does it matter at all that I'm connecting to a database that is hosted by a different company, under a different domain? So it would be a remote connection. Is a datasource still the best approach or does that matter?


I have rarely used remote connections. But I don't know if it makes a difference here. IIRC the connection string (or settings) would be the same whether you are using a datasource or a dynamic connection. In cases where there is no difference, a datasource is usually preferable.
User is offlineProfile CardPM
+Quote Post

jeffmc21
RE: PHP Within Coldfusion
25 Aug, 2008 - 12:42 PM
Post #9

New D.I.C Head
*

Joined: 27 Nov, 2007
Posts: 47


My Contributions


I went back and now I'm using the same server for the database table that I am for the rest of the site (my hosting co wouldn't allow remote connections).
So I added the necessary table to my already existing database.

So, using the CF code from your earlier posts, how would I connect to the database/table? The SQL isn't my problem (yet). I'm getting the error at line 3, because I don't know what to put for my datasource name. I already have a datasource that connects to the database to display and operate the rest of the site. It's specified in my Application.cfm file. How do I connect this call to the database?
User is offlineProfile CardPM
+Quote Post

sansclue
RE: PHP Within Coldfusion
26 Aug, 2008 - 03:42 AM
Post #10

D.I.C Head
**

Joined: 21 Nov, 2007
Posts: 113



Thanked: 7 times
My Contributions
QUOTE(jeffmc21 @ 25 Aug, 2008 - 01:42 PM) *

I went back and now I'm using the same server for the database table that I am for the rest of the site (my hosting co wouldn't allow remote connections).
So I added the necessary table to my already existing database.

So, using the CF code from your earlier posts, how would I connect to the database/table? The SQL isn't my problem (yet). I'm getting the error at line 3, because I don't know what to put for my datasource name. I already have a datasource that connects to the database to display and operate the rest of the site. It's specified in my Application.cfm file. How do I connect this call to the database?


Yes, some hosting companies won't allow remote connections.

If all of your tables are in the same database, and you already have a datasource for that database, just use that datasource name in the cfquery.

<cfquery name="getArticle" datasource="yourDatasourceNameHere">

Or if you're storing the datasource in a variable, use that variable name

<cfquery name="getArticle" datasource="#variableNameHere#">

I cannot be more specific because I don't know what kind of variable you are using to store datasource name. ie You could be using an application variable (#application.datasourceName#), request variable (#request.datasourceName#), etc...


User is offlineProfile CardPM
+Quote Post

jeffmc21
RE: PHP Within Coldfusion
26 Aug, 2008 - 08:18 AM
Post #11

New D.I.C Head
*

Joined: 27 Nov, 2007
Posts: 47


My Contributions
I've had to reorganize my SQL call because its MSSQL (my first time with it), and several of the commands I was using don't work in MSSQL. Specifically, the LIMIT command doesn't work, so I've had to use

SELECT TOP 3 * FROM table_name


This works fine now, because there aren't more than three rows in the database. However, as the database populates (from an online form), I will actually want to get the three most recent entries (the bottom 3). Are there MSSQL commands to accomplish this?

User is offlineProfile CardPM
+Quote Post

sansclue
RE: PHP Within Coldfusion
26 Aug, 2008 - 08:37 AM
Post #12

D.I.C Head
**

Joined: 21 Nov, 2007
Posts: 113



Thanked: 7 times
My Contributions
QUOTE(jeffmc21 @ 26 Aug, 2008 - 09:18 AM) *

I've had to reorganize my SQL call because its MSSQL (my first time with it), and several of the commands I was using don't work in MSSQL. Specifically, the LIMIT command doesn't work, so I've had to use

SELECT TOP 3 * FROM table_name


This works fine now, because there aren't more than three rows in the database. However, as the database populates (from an online form), I will actually want to get the three most recent entries (the bottom 3). Are there MSSQL commands to accomplish this?



Yes, just add back the ORDER BY clause and use DESC to get the three records with the most recent dates.


SELECT TOP 3 *
FROM table_name
ORDER BY event_date DESC
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 02:03PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month