I cannot get output data from a python script to php. There is no error thrown in the php, and the python script outputs perfectly from the command line. Python:
import json,sys,MySQLdb
db = MySQLdb.connect(host="localhost", user="root", passwd="root", db="sr")
cur = db.cursor()
def requestQuiz():
sql = "SELECT quiz FROM wordbank ORDER BY RAND() LIMIT 1"
cur.execute(sql)
result_set = cur.fetchall()
quiz = result_set[0][0]
return json.dumps(quiz)
if __name__ == "__main__":
#print solve(sys.argv[1])
print requestQuiz()
PHP
<?php
$result = exec("python request.py");
$results_array = json_decode($result);
echo "<p>".$results_array."</p>";
?>
python should return a single word in quotes, such as "happy"
And it does so when I tried it from command line
python request.py
I've confirmed that the php and python are in the same directory and the python script named "request.py"
.
– Tiancheng Xu Jan 27 '17 at 02:42