2

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. It works as desired.

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()

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'"?

2 Answers 2

1

If I understand it correctly, you are storing all data in one field of the table? If so, MySQL will not be of much use as you need to unserialize the data in python. If so, then this question is not really for DBA, but more for StackOverflow.

If you however want to restructure the table and keep various data elements in different columns instead of one column called "data", then you could SELECT only that particular column(s) that are relevant instead of querying whole string.

1
  • Thank you for the reply guys. The solution best suitable for me(i guess because I din't get started on it yet) is phpserialize. What do you guys think of that? Once I solve it I'll post the solution here. Mar 12, 2014 at 8:54
-1

You need to use the limit statement! This will return only one result set.
Since you have more recourds for entries_id = 5 without the limit statement you will get all the results.
Try the example bellow:

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 limit 1")
    rows = cur.fetchall()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.