0

I'd like to know if I can use PHP in order to get data from a MySQL database. A fraction of the code can be seen here:

<?php
$servername = "localhost";
$username = "root";
$password = "pass";
$dbname = "name";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT aa, bb, cc FROM data";
$result = $conn->query($sql);
...
?>

This is placed inside an HTML file in the Play Framework folder "views", and is properly loaded by the controller, but when it loads, it just shows me the code as if it were text, and not code or the action that would be supposed to do, so it is like it does not recognise it. How can I solve it?

jquery_stack
  • 261
  • 4
  • 17
  • 1
    Did you save it as an .php file? – PHPhil Jul 08 '15 at 11:24
  • The whole point of using an MVC framework like Play is *to not* mix your view/model logic together by making a database call in something that also renders html. Play has facilities for connecting to and executing queries on a database. – Michael Zajac Jul 09 '15 at 03:05
  • That is true Sir, but what is the way to get updating data (increasing database data) in order to plot it in a "real-time way"? – jquery_stack Jul 09 '15 at 08:17

3 Answers3

4

No, you cannot use PHP inside templates, Play doesn't parse PHP at all, actually it doesn't even know there is something like PHP.

P.S. Trying to reuse PHP code in your Java app will be much more difficult than learning the valid approach with Java only, see answer for similar post (it's about MySQL raw access, not PHP integration) which you can reuse in several minutes: https://stackoverflow.com/a/31118795/1066240

As bjfletcher mentioned you would need to configure both runtimes to be able work with Java and PHP at one server but it will NOT allow you for using PHP in Play's templates anyway! so it doesn't make deeper sense.

Community
  • 1
  • 1
biesior
  • 55,576
  • 10
  • 125
  • 182
  • The problem is that I'd need the data from a "running database" taken every "x" seconds to plot it using Flot Charts, which is sort of a "Real-time operation". What would be the best approach to achieve this purpose in Play Framework? – jquery_stack Jul 08 '15 at 20:28
  • Sorry, I don't know the Flot Charts, with Play you can easily access ANY database, also MySQL (as pointed in other answer) you can also use Akka Scheduler (which is some kind of __cron task__ known from unix) – biesior Jul 08 '15 at 22:39
  • I have successfully accessed the MySQL database but the problem is to get the values from the database in "real-time". That means, the data base will be "dynamic" and updating every "x" seconds and I need to update the values of this database and store them in arrays to plot them. Any ideas on what would be the best option? – jquery_stack Jul 09 '15 at 08:19
1

Play doesn't know PHP.

You need two runtimes:

  1. Play runtime
  2. PHP runtime

and use HTTP for integration. For example, if PHP code is on http://localhost/products then Play would send a GET request to this URL for a response. Play can then use this response to do whatever you want.

For example:

def index() = Action {
    WS.get("http://localhost/products").get.map { resp =>
      Ok(views.html.index(resp.body))
    }
}

then in your view template:

@(resp: String)

<h1>Products</h1>
@resp
bjfletcher
  • 11,168
  • 4
  • 52
  • 67
  • So what do I need to do to call this php code from my html file in the views folder? Is there any way to use my php code from this html file? Thanks – jquery_stack Jul 08 '15 at 13:17
  • See updated answer for some example of how to do that. – bjfletcher Jul 08 '15 at 14:34
  • So this would access to the products.php file and then what would be the output in the html file? I do not understand what kind of data or structure represents the last code lines that you've written in the answer. Thanks again for your input in advance :) – jquery_stack Jul 08 '15 at 20:32
  • Play doesn't access `products.php` - you will see the strange PHP syntax in the HTML page :) It needs to go through the PHP runtime first. – bjfletcher Jul 08 '15 at 20:35
-1

Best thing to do is write it totally in php. Forget about using the bloated playframework and trying to figure out all the connections that you would have to make to tie it all together.

klmbear
  • 83
  • 2
  • 5