Query to Access a specfic data in the row using python

133 views Asked by At

Am using wordpress and phpmyadmin for handling my database. Below is the code I use to view entries on my form using python. In the form I ask users to check/uncheck specific questions.

import MySQLdb as mdb
con = mdb.connect('localhost', 'root', 'password', 'WordpressDB', unix_socket="/opt/lampp/var/mysql/mysql.sock");
with con: 
    cur = con.cursor()
    cur.execute("SELECT data FROM `wp_form_builder_entries` WHERE entries_id = 5")
    rows = cur.fetchall()
    print rows

Output generated by this code is as follows :

enter image description here

Works just as desired but I don't want all the data. I just need to know whether its checked or unchecked. If the user has checked value is "s:9:'checked'" else its "s:9:'unchecked'"

What query should I use to retrieve "s:9:'checked'" ?

Please advice

Thank you

2

There are 2 answers

0
geertjanvdk On

You can not simply do this using a SQL Query. You have to deserialise the PHP data using phpserialize and get the data from it. See also other answer.

0
Michał Niklas On

At first you can show us what is in one row (your data column is in row[0]):

for row in rows:
    print('%s' % (row[0]))

Then analyze what is interesting in this column. Make separate function that converts this column data into something you want to display:

def get_interesting_data(data_column):
    data_str = '%s' % data_column
    ... # use some regexp or string function
    return data_str

And use it:

for row in rows:
    print('%s' % (get_interesting_data(row[0])))