-1

I am using Raspberry Pi, python and PHP. I can't get my count(output) into my php (I am using apache2). I code my raspberry pi in terminal (command prompt). I have tried using exec() and shell_exec(), however there is no output.

This are the code I have tried

exec("python /home/pi/testing.py");

$file = popen("/home/pi/testing.py","r");
echo $file
pclose($file);



<?php
#command = escapeshellcmd('/home/pi/testing.py');
$output = shell_exec($command);
echo $output;
?> 

<!DOCTYPE html>
<html>
<head>
<style>
table,th,td {
border:1px solid black;
}
</style>
</head>
<body>

<?php

echo "<br>";
echo date ('y-m-d H:i:s');

$conn = new mysqli("localhost","root","password","menagerie");

if($conn->connect_error)
{ 
die("connection failed:" . $conn->connect_error);
}

$sql = "SELECT * FROM people";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    echo "<table><tr><th>Vistor</th><th>Entry</th></tr>";
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>" . $row["vistor"]. " </td><td> " . $row["Date"]. "
</td></tr>";
    }
echo"</table>";
}
$conn->close();

?>

""<br>
""<br>


</body>
</html>
peterh
  • 11,875
  • 18
  • 85
  • 108
nurul98
  • 117
  • 3
  • 10
  • I think following [these trivial rules](https://meta.stackoverflow.com/a/291370/1783163) could make your posts much better with only a little work. – peterh Jun 17 '17 at 12:16

1 Answers1

0

In Python file 'test.py' verify this text is in the first line: (see shebang explanation):

#!/usr/bin/env python

Also Python file should have correct privileges (execution for user www-data / apache if PHP script runs in browser or through curl) and/or must be "executable". Also all commands in .py file must have correct privileges.

chmod +x myscript.py

  • Hi i have tried to insert #!/usr/bin/env python into my py, however when i execute ch +x inside command prompt it does not have any action of effect. As i have google chmod +x myscript.py i suppose it functions as a python. myscript.py? https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=26894 i also have tried to run the myscript.py and the webpage together. It did not work as well. – nurul98 Jun 16 '17 at 04:53
  • Okay, two questions. One, what happens when you just type in the command line `python my script.py`? And also, what is the goal of the python script? Why aren't you just writing the code in php rather than calling python within php? – Doug Skinner Jun 16 '17 at 05:02
  • Hi sir when i type in command python myscript.py, it is able to do adding and subtraction by 1 on the raspberry pi. As i could not link the raspberry pi GPIO pin to PHP i am unable to write the codes inside php. – nurul98 Jun 16 '17 at 05:17
  • Okay. In that case, I think you might have to convert your php code into python. Converting from one programming language to another is never going to be the best idea. Try taking a look at [flask](http://flask.pocoo.org) – Doug Skinner Jun 16 '17 at 15:38