We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › MySQL / PHP / TomCat / Processing
Page Index Toggle Pages: 1
MySQL / PHP / TomCat / Processing (Read 5341 times)
MySQL / PHP / TomCat / Processing
Dec 5th, 2005, 6:13am
 
Hi,

I'm trying to get my head around making a processing applet that communicates with MySQL. Processing cannot communicate directly with an MySQL server because the applet is running on the clients machine, which is not a 'trusted host' for communicating with the MySQL server, correct???

So in order to have my Applet talk to MySQL, I should send all my queries to some PHP and have that talk to the database, and spit it back to processing???

I've also heard a bit about 'servlets' in the forums. Is that another way to do it?

I am new to MySQL and processing, and have a bit of experience with PHP. MUCH help is appreciated.
Re: MySQL / PHP / TomCat / Processing
Reply #1 - Dec 6th, 2005, 12:43pm
 
If you sign your applets they can access any server. The downside is that you get the annoying and scary "Would you buy a used car from this man?" requester before running the applet.
Re: MySQL / PHP / TomCat / Processing
Reply #2 - Jan 5th, 2006, 2:42am
 
hi

it's really quite easy to use a PHP script to communicate between MySQL and p5.  no need to sign the applet or talk to tomcat unless you're sending too much data for a GET request (the maximum length of a URL for the server, which i think is by default about 2000 characters on iis/windows servers and about 8000 on apache/*nix servers).

i believe it's actually more secure to handle the databse stuff in a php script than in p5 because a java applet can potentially be decompiled and then your MySQL login/password/server is available to be borrowed/stolen/h4xx0red.

to talk to mysql through php, i just use loadStrings(url) where the String url has been composed of data i want to insert into the database.

for example:
Code:

String url = "http://domain.com/insert.php?color=" + theColor + "&size=" + theSize;
loadStrings(url);


that loadStrings() is just to get p5 to send the request; the script doesn't actually return any text to it.

the insert.php file will look something like this:
Code:
 // insert.php
<?PHP
$dblink = mysql_connect("mysqldatabase.domain.com", "mysql_username", "mysql_password");
mysql_select_db("name_of_database");

$color = $_GET['color];
$size = $_GET['size'];

$sql = "INSERT INTO squares SET size='$size' color='$color'";
$result = mysql_query($sql);
?>


then to read out all squares from the database, i'll have a script called 'read.php' or something that looks like this:
Code:
 // read.php
<?PHP
$dblink = mysql_connect("mysqldatabase.domain.com", "mysql_username", "mysql_password");
mysql_select_db("name_of_database");

$sql = "SELECT * FROM squares";
$result = mysql_query($sql);

while($row = mysql_fetch_array($result)) {
echo $row['color'];
echo " || ";
echo $row['size'];
echo "\n";
}
?>


the output is easy to parse with p5's split().

keeping the database access logic on the server has the additional advantage that if you need to change your database structure or something you can do so without requiring users to download a new client.

hope this is helpful!
-jake
Re: MySQL / PHP / TomCat / Processing
Reply #3 - Apr 15th, 2008, 2:47pm
 
I understand the PHP, but how do i get processing and PHP talking??
Re: MySQL / PHP / TomCat / Processing
Reply #4 - Apr 15th, 2008, 3:26pm
 
You can use loadStrings and give it a URL with parameters to send, e.g.
Code:
String result=loadStrings("http://www.myhost.com/foo/wibble.php?foo=bar&ping=quux");


And your wibble.php can talk to MySQL do whatever it needs, and return the result to processing as a String[] to handle however you like.
Re: MySQL / PHP / TomCat / Processing
Reply #5 - Apr 15th, 2008, 5:07pm
 
How do i get the data after i update the database with php with the read.php??
Re: MySQL / PHP / TomCat / Processing
Reply #6 - Jun 10th, 2008, 10:43pm
 
I'm having the same problem. I don't want anything more to happen than this Code:
lines = loadStrings("data_grabber.php"); 

but there's a delay I think that's messing it up...

Here's where I posted roughly the same problem earlier today. If I find the answer, I'll let you know.
Re: MySQL / PHP / TomCat / Processing
Reply #7 - Jun 11th, 2008, 2:11am
 
This should work

String url = "url of the php script";
 String [] position;
 position = loadStrings(url);

The load strings loads the script and then from the php you use an echo command and then capture the results in an array
Re: MySQL / PHP / TomCat / Processing
Reply #8 - Jun 11th, 2008, 5:07pm
 
Evidently my problem was a lot harder to solve because an old applet is stuck in my firefox cache.. even after using the java control panel to flush out its temp files, it sticks. Once I got your code working with the run button (flakey.. but magically worked finally when i'm sure i changed nothing) it worked in IE. I appreciate the help. Thanks! Cheesy
Page Index Toggle Pages: 1