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 › PHP -> MySQL optimization issue
Page Index Toggle Pages: 1
PHP -> MySQL optimization issue (Read 1877 times)
PHP -> MySQL optimization issue
Feb 19th, 2010, 9:17am
 
Hey guys,

So i've been working on a piece that will be hosted online.  The user will draw something, and this is added to a database of drawings.  I've got everything working; the user can draw, the data gets passed via php to my database, and the sketch can read from the database and draw the images on the screen.  

Despite it working, there is a big issue of loading time.  I'm entering the information into the DB as strings that contain information to re-draw the image (ie. mouse cords, size, color).  When I retrieve the info from the DB it can gather all the data and draw a handful of images in seconds, however when I upload the data it can take a few minutes for one drawing.  

I've been using the GET method in PHP and passing one line at a time in a for loop via loadStrings, and I think this is the issue.  I believe it is connecting to my DB every time, which makes it incredibly slow.  I tried just passing a giant string with all the data at once but it gave me a http 414 error (url too long).  Here is my php code, any suggestions would be greatly appreciated.  Thanks.

Code:
<?PHP

$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';
$dbname = 'name';
$dbtable = 'info';

$dblink = mysql_connect("$dbhost", "$dbuser", "$dbpass") or die ('I cannot connect to the database because: ' . mysql_error());


mysql_select_db("$dbname");


$id = $_GET['id'];
$xcord = $_GET['xcord'];
$ycord = $_GET['ycord'];
$brush = $_GET['brush'];
$alpha = $_GET['alpha'];
$color = $_GET['color'];


$sql = "INSERT INTO info (id,xcord,ycord,brush,color,alpha) VALUES ('$id', '$xcord', '$ycord', '$brush', '$color', '$alpha')";
$result = mysql_query($sql);

?>
Re: PHP -> MySQL optimization issue
Reply #1 - Feb 19th, 2010, 9:47am
 
look at the other php / mysql interfaces. i believe there's a mysql_pconnect which uses a persistent connection.

(although that might not help if you're only doing one insert per invocation of the php)

that said, they recommend you use the mysqli (improved) libraries rather than the mysql libraries
Re: PHP -> MySQL optimization issue
Reply #2 - Feb 19th, 2010, 10:08am
 
russell wrote on Feb 19th, 2010, 9:17am:
http 414 error (url too long)

In this case, switch to POST mode...
Re: PHP -> MySQL optimization issue
Reply #3 - Feb 19th, 2010, 1:30pm
 
I'm not too familiar with php, so how would I go about switching to POST mode?  In my loading class my code looks like this,

Code:
for(int z=0; z<totalCount; z++){
   //return line:  "&xcord=" + x+ "&ycord=" + y+ "&brush=" + sizeVal+ "&alpha=" + alphaVal+ "&color=" + colorVal;
    currentLine = canvas.getLine(z);
    tempUrl = "http://russellgoldenberg.com/clouds/insert.php?id=" + id + currentLine;
 
    loadStrings(tempUrl);
}


Can i tack on an array to the end of the url and use loadStrings and then change GET to POST in the php, thereby avoiding the loop and only calling loadStrings once or am I missing something?  Thanks.
Re: PHP -> MySQL optimization issue
Reply #4 - Feb 19th, 2010, 2:05pm
 
Indeed you should pass all data to PHP in one shot. Each time you call loadStrings(), your applet needs to resolve an URL, run your PHP script which connects to the database, and proceed one query. This is too slow if you have a lot of data.

You can pass the data though a POST request, or save it in a text file on the server, that will later be read by PHP. (I'm searching for more info about POST requests in Java...)
Re: PHP -> MySQL optimization issue
Reply #5 - Feb 19th, 2010, 2:15pm
 
In Re: save to web --- image export I show how to do a POST request on Java / Processing side, and how to handle it on PHP side (although it is a file being uploaded...).
On the PHP side, just replace $_GET[] with $_POST[].
Re: PHP -> MySQL optimization issue
Reply #6 - Feb 19th, 2010, 2:43pm
 
Here is a light example:

Processing
Quote:
String data = "x[]=50&x[]=100";
try {
  URL url = new URL("http://localhost/tests/showpost.php");
  URLConnection conn = url.openConnection();
  conn.setDoOutput(true);
  PrintWriter output = createWriter(conn.getOutputStream());
  output.write(data);
  output.flush();
  StringBuffer answer = new StringBuffer();
  BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  String lines;
  while ((lines = reader.readLine()) != null) {
    answer.append(lines);
  }
  output.close();
  println(answer.toString());
catch(Exception e) {
  println("An error has occured");
}


PHP
Code:
<?php
mysql_connect('localhost', 'root', '');
$x[0] = mysql_real_escape_string($_POST['x'][0]);
$x[1] = mysql_real_escape_string($_POST['x'][1]);
mysql_query("INSERT INTO `my_database`.`my_table` (`x_0`, `x_1`) VALUES(Now(), '{$x[0]}', '{$x[1]}');");


Notice that the brackets in x[]=50&x[]=100 allows you to pass all values in a simple array.
Re: PHP -> MySQL optimization issue
Reply #7 - Feb 19th, 2010, 3:27pm
 
awesome guys.  I ran that last little snippet of code on my server and it worked with some tweaks.  

Thanks so much for the quick and helpful responses.

Russell
Page Index Toggle Pages: 1