Page 1 of 1

Trouble with PHP running on RP server

Posted: Tue Mar 01, 2016 9:17 am
by minhthanhvn
Hi guys!
I'm working on a Automatic Home project with a Raspberry works as a server and several Arduino clients. I created a database with SQLite on Raspberry but when I'm working with PHP web interface, I encountered a strange behavior of my PHP script. This is it:

Code: Select all

<html>
 <head>
  <title>PHP Test</title>
 </head>
 
 
 <body>
    <?php

      
      $db = new SQLite3('/home/pi/webapp/DB.db');
      
      
      $result = $db->query('SELECT * FROM temps;');
      
      while ($res = $result->fetchArray(SQLITE3_ASSOC)){
              
              print ("Date: " . $res["date"] . " ");
              print ("Date: " . $res["time"] . " | ");
              print ("Temperature: " . $res["temperature"] . " <br \>");
              }
      $db->close();
    ?>

</body>
</html>
Instead of showing a webpage, it only shows this weird result in browser:

Code: Select all

query('SELECT * FROM temps'); while ($res = $result->fetchArray(SQLITE3_ASSOC)){ print ("Date: " . $res["date"] . " "); print ("Date: " . $res["time"] . " | "); print ("Temperature: " . $res["temperature"] . " 
"); } $db->close(); ?>
Thanks in advance. :D

Re: Trouble with PHP running on RP server

Posted: Tue Mar 01, 2016 10:33 am
by rurwin
I can see what it's doing, maybe that will help.

It sees the ">" in "$result = $db->query(..." as closing the HTML tag opened in "<?php".

I would guess that the page is not being passed to the PHP interpreter but is instead being treated as pure HTML.

Re: Trouble with PHP running on RP server

Posted: Tue Mar 01, 2016 10:38 am
by 0xFF
Is PHP enabled in your http server (Apache)?
Try

Code: Select all

<?php
phpinfo()
?>

Re: Trouble with PHP running on RP server

Posted: Tue Mar 22, 2016 10:44 pm
by rottjr
0xFF wrote:Is PHP enabled in your http server (Apache)?
Try

Code: Select all

<?php
phpinfo()
?>
Dont forget the semicolon.

Re: Trouble with PHP running on RP server

Posted: Thu Mar 24, 2016 9:06 am
by BanofiPi
Even without the semicolon, the above script should work.
Which suggests that the php installation is not present or corrupt.
About 10 days ago I installed Raspian, Apache2 and PHP5 on my new RPi3, without a problem.
Yesterday (23rd March) I repeated the procedure. Raspian and Apache2 - no problem - It worked.
However, PHP was a different story.

This is what I tried to execute and the result:
pi@raspberrypi:/ $ sudo apt-get install php5 libapache2-mod-php5 -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libonig2 libperl4-corelibs-perl libqdbm14 lsof php5-cli php5-common
php5-json php5-readline
Suggested packages:
php-pear php5-user-cache
The following NEW packages will be installed:
libapache2-mod-php5 libonig2 libperl4-corelibs-perl libqdbm14 lsof php5
php5-cli php5-common php5-json php5-readline
0 upgraded, 10 newly installed, 0 to remove and 2 not upgraded.
Need to get 5,026 kB of archives.
After this operation, 18.6 MB of additional disk space will be used.
WARNING: The following packages cannot be authenticated!
libonig2 libperl4-corelibs-perl lsof libqdbm14 php5-common php5-json
php5-cli libapache2-mod-php5 php5 php5-readline
E: There are problems and -y was used without --force-yes
Obviously something has changed very recently.
I invite comment.

Re: Trouble with PHP running on RP server

Posted: Thu Mar 24, 2016 3:14 pm
by rottjr
I would say a bad source list. My source list get's screwy sometimes. Either that or check the date/time on the Pi, for some reason those packages aren't coming through as valid. My source list is below

/etc/apt/sources.list

Code: Select all

deb http://mirrordirector.raspbian.org/raspbian/ jessie main contrib non-free rpi
# Uncomment line below then 'apt-get update' to enable 'apt-get source'
#deb-src http://archive.raspbian.org/raspbian/ jessie main contrib non-free rpi
deb http://apt.adafruit.com/raspbian/ jessie main
EDIT: Make sure to run apt-get update if you change your source list
NOTE: It's always a good idea to run apt-get update before installing new packages.

Also, the -y shouldn't be needed unless the package has weird dependencies. I typically install PHP on debian, not just pi, by doing the below command

Code: Select all

apt-get install php5 php5-mysql
Obviously the second line is only needed if you want mysql extensions.

Hope it helps,
-RottJR