0

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.

I can already UPDATE the MySQL database when I run the following PHP script on the webserver:

<?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.

The PHP file seems to be publicly accessible so I don't imagine webserver credentials are required in my Python?

Thank you in advance SO!

3
  • What have you tried to do in Python? The requests library is popular for making HTTP requests from Python, and should be straight forward to call with the URL to your PHP script (import requests, requests.get('https://example.com/foo.php') Commented Oct 18, 2021 at 21:31
  • Hi, thanks for your reply. Does that Python you've posted have to target a PHP file that runs a GET? Currently all that's returned by requests.get('https://example.com/foo.php') is a 200 Commented Oct 18, 2021 at 21:35
  • @INoble yes it would. But python is perfectly capable of sending POSTs too. A bit of simple research will get you there... (as well as the answer below) Commented Oct 18, 2021 at 21:39

2 Answers 2

2

With a local PHP server running using php -S localhost:8881 receive.php

send.py

import requests

url = 'http://localhost:8881'

myobj = {'key1': 'value1'}

x = requests.post(url, data = myobj)

print (x.text)

receive.php

<?php

var_dump($_POST);

Output of running send.py will be:

array(1) {
    ["key1"]=>
    string(6) "value1"
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you that works perfectly for a GET Can you help me out with a POST also? The main thing I'm looking to do is make changes, via Python -> PHP -> MySQL
0

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.

I write the integer 6 where the value 2 exists in the database table.

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
$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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.