1

ive been trying to pass value from my php to python to save to my database but my sys.argv[1] is not receiving any value.

import firebase_admin
from firebase_admin import credentials, db
import mysql.connector
from datetime import datetime
import sys

# Initialize Firebase Admin SDK
cred = credentials.Certificate("D:\XAMPP\htdocs\EVA\dashmin-1.0.0\inventory-database-2ede2-firebase-adminsdk-sj5na-be229367e1.json")
firebase_admin.initialize_app(cred, {
    'databaseURL': 'https://inventory-database-2ede2-default-rtdb.firebaseio.com/'
})

# Connect to MySQL database
mydb = mysql.connector.connect(
    host="localhost",
    user="root",
    password="",
    database="inventory_db"
)

# Retrieve data from SQL table
branch_name = sys.argv[1]
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM transac WHERE branch = %s", (branch_name,))
data = mycursor.fetchall()
    

# Insert data into Firebase Realtime Database
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
ref = db.reference(timestamp)
trans = "transaction"
for row in data:
     ref.child(trans).push({
        'ID': row[0],
        'proname': row[1],
        'quantity': row[2],
        'price': row[3],
        'branch': row[4]
    })

and this my php variable

        // Execute Python script
        $branch_name = $user_data['branch'];
        $command = "python " . __DIR__ . "/mySnakecode.py " . escapeshellarg($branch_name);
        exec($command, $output, $return_var);

        if ($return_var !== 0) {
            echo "Error: Python script returned non-zero exit code";
        } else {
            echo "Python script output:\n" . implode("\n", $output);
        }

Ive tried it without the sys and it works but i really need the sys to work in order to get the value from my php variable to use in my php command in my python script.

  • After this line `$branch_name = $user_data['branch'];` try printing or echoing the variable `$branch_name` and check like `print_r($branch_name);exit;`. Tell me what you are getting? –  May 15 '23 at 11:03
  • I still get IndexError: list index out of range but i checked the firebase its still receiving the data so i think it failed successfully – Jezreel Honrada May 15 '23 at 12:15

0 Answers0