0

I have a function in file background.py

#! /usr/bin/python
import sqlite3
db = sqlite3.connect("vt.db")
cr=db.cursor()
def content():
icerik =""
sql_sorgusu= db.execute("SELECT * FROM icerik").fetchall()
if len(sql_sorgusu = 0):
    print("")
else:
    icerik +="""
            <div class="article"><img src='""" + str(i[3]) + """' alt="Bilişim Hukuku" class="img"><a href="" class="categori">Bilişim Hukuku</a><br><h3 class="articleh3">""" + str(i[1])+ """</h3><a href='/blog.html?id=""" + str(i[0])+ """'>Devamını oku</a></div>
    """
return icerik

and i want the run this function in my php file. I know the command shell_exec and exec but they are not running the function or idk how to do that can somebody help me.

  • https://stackoverflow.com/questions/19735250/running-a-python-script-from-php – Fresco Jan 28 '21 at 16:02
  • this is not i what i am asking for but thanks for your answer – Doğan Ali ŞAN Jan 28 '21 at 16:24
  • https://www.php.net/manual/en/function.shell-exec.php have you tried this? – Fresco Jan 28 '21 at 16:27
  • I think i must be more spesific, in file background.py there wont be just one function after function content there will be others so i must be able to call each one of them in another place but shell exec or exec command just calling the file not the function or idk know how to do that. I couldn't see what i want in the links you sent. – Doğan Ali ŞAN Jan 28 '21 at 16:32

2 Answers2

0

I don't know how to embed Python code in PHP this python code


#! /usr/bin/python
import sqlite3

db = sqlite3.connect("vt.db")
cr = db.cursor()

icerik = ""
sql_sorgusu = db.execute("SELECT * FROM icerik").fetchall()

for i in sql_sorgusu:
    if len(i) >= 1:
        icerik += f"""
        <div class="article">
            <img src='+ {str(i[3])}' alt="Bilişim Hukuku" class="img">
            <a href="" class="categori">Bilişim Hukuku</a>
            <br>
            <h3 class="articleh3"> {str(i[1])}</h3>
            <a href='/blog.html?id= {str(i[0])}'>Devamını oku</a>
        </div>
        """
    else:
        print("not SQL match !")

print(icerik) # complete html code
  • I did explain myself better in other answer but let me explain it to you too, i am creating a function for call it in another place your code is should work well but i cant put another functions in this file even i put them i wont be able to call them in another place. Thats why i can't use shell_exec or exec because they are just reading the file but not running what is inside the file – Doğan Ali ŞAN Jan 28 '21 at 16:36
  • ok you want some functions for calling them from other files – every skills Jan 28 '21 at 16:46
0

I hope that is what you want

#! /usr/bin/python
import sqlite3

db = sqlite3.connect("vt.db")
cr = db.cursor()

def get_db_data():
    return db.execute("SELECT * FROM icerik").fetchall()

def get_result_html():
    icerik = ""
    for i in get_db_data():
        if len(i) >= 1:
            icerik += f"""
            <div class="article">
                <img src='+ {str(i[3])}' alt="Bilişim Hukuku" class="img">
                <a href="" class="categori">Bilişim Hukuku</a>
                <br>
                <h3 class="articleh3"> {str(i[1])}</h3>
                <a href='/blog.html?id= {str(i[0])}'>Devamını oku</a>
            </div>
            """
        else:
            print("not SQL match !")

    return str(icerik)

  • My problem is i can't call this function in my php file i dont have any problems in my python code. I am asking for how can i call this functions in php file. But thank you a lot for trying to give me an answer. – Doğan Ali ŞAN Jan 28 '21 at 16:58
  • I can't help you sorry, I am not good at PHP! – every skills Jan 28 '21 at 17:09