Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中调用Python代码并显示输出_Python_Php - Fatal编程技术网

在PHP中调用Python代码并显示输出

在PHP中调用Python代码并显示输出,python,php,Python,Php,当我试图在一个PHP插件中运行Python代码时,我一直在与PHP-Python交互作斗争 我在PHP中尝试了许多不同的方法来调用Python代码,例如escapeshellcmd/shell_exec,它只提供“打印”输出,甚至不能插入换行符或以其他方式显示连续单行以外的数据;也尝试过使用,但没有得到任何结果,不知道我是否正确使用了它 我只想能够从我的PHP块调用Python代码,并将图形之类的东西显示为输出。目前,我只能从数据库中输出数据,我正在收集数据作为“打印”,这有点混乱 编辑: im

当我试图在一个PHP插件中运行Python代码时,我一直在与PHP-Python交互作斗争

我在PHP中尝试了许多不同的方法来调用Python代码,例如escapeshellcmd/shell_exec,它只提供“打印”输出,甚至不能插入换行符或以其他方式显示连续单行以外的数据;也尝试过使用,但没有得到任何结果,不知道我是否正确使用了它

我只想能够从我的PHP块调用Python代码,并将图形之类的东西显示为输出。目前,我只能从数据库中输出数据,我正在收集数据作为“打印”,这有点混乱

编辑:

import MySQLdb

db = MySQLdb.connect(host="localhost",    # your host, usually localhost
                     user="user",         # your username
                     passwd="password",  # your password
                     db="moodle")        # name of the data base

# you must create a Cursor object. It will let
#  you execute all the queries you need
cur = db.cursor()

# Use all the SQL you like
cur.execute("SELECT * FROM mdl_pibic_form")

# print all the first cell of all the rows
for row in cur.fetchall():
    print row
PHP代码(我称之为外部python代码的部分):

Python代码:

import MySQLdb

db = MySQLdb.connect(host="localhost",    # your host, usually localhost
                     user="user",         # your username
                     passwd="password",  # your password
                     db="moodle")        # name of the data base

# you must create a Cursor object. It will let
#  you execute all the queries you need
cur = db.cursor()

# Use all the SQL you like
cur.execute("SELECT * FROM mdl_pibic_form")

# print all the first cell of all the rows
for row in cur.fetchall():
    print row

这是工作代码,只有“打印”。只要我尝试将其解析为一个简单的表,并在for中打印“\n”字符,它就不会应用于输出。我还尝试调用简单的图形绘图代码(尽可能简单),但没有提供任何输出。

您能输入您尝试过的代码吗?抱歉,刚刚添加了这些代码。请尝试编写python脚本,将输出编码为JSON。在PHP中获取结构数据会更容易。我会研究一下,谢谢。