[英]How do I pass a variable from my local Python code to a remote PHP script that writes to a MySQL database?
On my local PC I want to use Python to both send and receive data on a remote MySQL database, via a PHP file that is located on the same webserver as the MySQL database.在我的本地 PC 上,我想使用 Python 通过与 MySQL 数据库位于同一网络服务器上的 PHP 文件在远程 MySQL 数据库上发送和接收数据。
I can already UPDATE the MySQL database when I run the following PHP script on the webserver:当我在网络服务器上运行以下 PHP 脚本时,我已经可以更新 MySQL 数据库了:
<?php
$host_name = 'host';
$database = 'db';
$user_name = 'user';
$password = 'pass';
$conn = new mysqli($host_name, $user_name, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE test
SET test = 1
WHERE test = 0";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
I have searched for hours but so far cannot make any decent attempt at Python code that will send a variable to the PHP code that will in turn update the MySQL database.我已经搜索了几个小时,但到目前为止还无法对 Python 代码进行任何体面的尝试,该代码将向 PHP 代码发送一个变量,该代码又将更新 MySQL 数据库。
The PHP file seems to be publicly accessible so I don't imagine webserver credentials are required in my Python? PHP 文件似乎可以公开访问,所以我认为我的 Python 中不需要网络服务器凭据?
Thank you in advance SO!提前谢谢你!
With a local PHP server running using php -S localhost:8881 receive.php
使用
php -S localhost:8881 receive.php
运行本地 PHP 服务器
send.py发送.py
import requests
url = 'http://localhost:8881'
myobj = {'key1': 'value1'}
x = requests.post(url, data = myobj)
print (x.text)
receive.php接收.php
<?php
var_dump($_POST);
Output of running send.py will be:运行 send.py 的输出将是:
array(1) {
["key1"]=>
string(6) "value1"
}
For future potential visitors, below is the combination of Python and PHP that finally wrote a value from my local Python file -> to the remote PHP file -> which wrote successfully to a MySQL database.对于未来的潜在访问者,下面是 Python 和 PHP 的组合,它们最终将值从我的本地 Python 文件 -> 写入远程 PHP 文件 -> 成功写入 MySQL 数据库。
I write the integer 6 where the value 2 exists in the database table.我写了整数 6,其中值 2 存在于数据库表中。
Python: Python:
import requests
url = 'https://url/file_name.php'
myobj = {'key1': 6}
x = requests.post(url, data = myobj)
print (x.text)
PHP on server (file_name.php):服务器上的 PHP (file_name.php):
<?php
$host_name = 'hostname';
$database = 'db_name';
$user_name = 'user_name';
$password = 'password';
$conn = new mysqli($host_name, $user_name, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Set variable post_data to equal the value of 'key1', which also had to be converted into an integer
$post_data = intval($_POST['key1']);
// SQL Query
$sql = "UPDATE table_name
SET column_name = $post_data
WHERE value = 2";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
I get the feeling there's probably a lot of useless information in the PHP file, but I'm a PHP newbie and just glad it's working now :)我觉得 PHP 文件中可能有很多无用的信息,但我是 PHP 新手,很高兴它现在可以工作了 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.