简体   繁体   English

如何将本地 Python 代码中的变量传递给写入 MySQL 数据库的远程 PHP 脚本?

[英]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.

相关问题 如何将我从远程MySQL数据库选择的一些行插入本地MySQL数据库 - how do I insert some rows that I select from remote MySQL database to my local MySQL database 我需要将变量从我的 python 脚本传递到远程服务器 - I need to pass variable from my python script to a remote server 如何将参数从 python 传递给 php 脚本? - How do I pass an argument to a php script from python? 如何在远程Spark集群上运行本地Python脚本? - How do I run a local Python script on a remote Spark cluster? 如何在python中使用mysql数据库调试cgi脚本。 我是如何调试的新手 - How do i debug my cgi script with mysql database in python. I am new to how to debugg 如何通过ajax调用使用php将变量或查询传递给python脚本? - How do I pass variable or query to a python script using php via an ajax call? 从(本地)python脚本运行(远程)php脚本 - Run (remote) php script from (local) python script 从本地 PHP 脚本运行远程 python 脚本 - Running remote python script from local PHP script 如何将变量的值插入 MySQL 数据库? - How do I insert the value of my variable into my MySQL database? 如何将变量从python脚本传递到php并在网页中显示 - How to pass variable from python script into php and show it in webpage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM