我无法从python脚本获取输出数据到php。 php中没有抛出错误,python脚本从命令行输出完美。 的的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应该在引号中返回单个单词,例如"happy"
当我从命令行尝试它时它会这样做
python request.py
我已经确认php和python位于同一目录中,而python脚本名为“request.py”
答案 0 :(得分:0)
尝试使用shell_exec()
。就像这样..
<?php
$result = shell_exec("python request.py");
$results_array = json_decode($result);
echo "<p>".$results_array."</p>";
?>