473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

MYSql, CGI web page search code not working

OK,

I can now successfully enter data into my MySQL database through my CGI
web page. I can click a button and retrieve all the records, but I can
not seem to get the search code to work.

Below is the web page code and then my Python script. When I click my
search button it just gives me all the records

I know this line is executing: cursor.execute("Select * from phone
where name = name order by name")

Because I played with the "order by" but it seems to ignore my where
clause.

No matter what I type in the form text box (or even if I leave it
blank) I get all the records.

I can hard code this line to: cursor.execute("Select * from phone where
name = 'Fred' order by name")

and it returns the one record corectly.

Any ideas?

Fred

------------------------------------------
<form action="cgi-bin/searchdata.py">
<p>Enter the name to find:
<p><input type="text" name="name" size="30">
<input type="submit" value="Search">
</form>
------------------------------------------

#!/usr/local/bin/python
print "Content-Type: text/html\n"
import MySQLdb
import cgi

db=MySQLdb.connect(host = 'localhost', db = 'phone')
cursor=db.cursor()
cursor.execute("Select * from phone where name = name order by name")

result = cursor.fetchall()
for record in result:
print '<p>'
print record[0]
print '--'
print record[1]
print '--'
print record[2]
print '--'
print record[3]
print '</p>'

Jan 28 '06 #1
10 2248
>
db=MySQLdb.connect(host = 'localhost', db = 'phone')
cursor=db.cursor()
cursor.execute("Select * from phone where name = name order by name")


You don't parametrize the query. The where-clause thus is a tautology,
as the name is always the name.

Do something like this:
cursor.execute("Select * from phone where name = ? order by name", (name,))
Actually it might be necessary to use something different from the ? to
specify the parameter - that depends on the paramstyle of your DB-Api.
Check that in the interpreter with

import MySQLdb
print mySQLdb.paramstyle

Diez
Jan 28 '06 #2
print MySQLdb.paramstyle returns: format

I found one example like this:

cursor.execute('''Select * from phone where name=%s order by
name''',(name))

But I get this in my Apache error log:
NameError: name 'name' is not defined

Like my last problem I posted, I am sure it is something very simple
that I am missing!!
Fred

Jan 28 '06 #3
Fred wrote:
No matter what I type in the form text box (or even if I leave it
blank) I get all the records.


Try this:

#!/usr/local/bin/python
print "Content-Type: text/html\n"
import MySQLdb
import cgi

db=MySQLdb.connect(host = 'localhost', db = 'phone')
cursor=db.cursor()
cursor.execute("Select * from phone where name=%s order by name", (name,))

result = cursor.fetchall()
for record in result:
print '<p>'
print record[0]
print '--'
print record[1]
print '--'
print record[2]
print '--'
print record[3]
print '</p>'

(Assuming the name of your text field is "name".)
Jan 28 '06 #4
Yeah, I already tried that (except you have a , after name.

Your code produces the same error:

NameError: name 'name' is not defined

I know I am close!! Just missing some small thing...

Jan 28 '06 #5
Fred wrote:
Yeah, I already tried that (except you have a , after name.

Your code produces the same error:

NameError: name 'name' is not defined

I know I am close!! Just missing some small thing...


Oh, duh. I forgot something:

#!/usr/local/bin/python
print "Content-Type: text/html\n"
import MySQLdb
import cgi

form = cgi.FieldStorage()

db=MySQLdb.connect(host = 'localhost', db = 'phone')
cursor=db.cursor()
cursor.execute("Select * from phone where name=%s order by name",
(form['name'].value,))

result = cursor.fetchall()
for record in result:
print '<p>'
print record[0]
print '--'
print record[1]
print '--'
print record[2]
print '--'
print record[3]
print '</p>'

The comma is intentional: the MySQLdb wants the argument(s) as a tuple.
Jan 28 '06 #6
Thanks Kirk! That worked perfect! And makes perfect since now that I
see it...

Now that I have the main pieces working I can start expanding from
here!

Fred

Jan 28 '06 #7
OK one more... how would I do a "LIKE" instead of a = in this code?

cursor.execute("Select * from phone where name=%s order by name",
(form['name'].value,))

Right off I think:

cursor.execute("Select * from phone where name like %%s% order by
name",
(form['name'].value,))

But it blows up...

Jan 28 '06 #8
Fred wrote:
OK one more... how would I do a "LIKE" instead of a = in this code?

cursor.execute("Select * from phone where name=%s order by name",
(form['name'].value,))

Right off I think:

cursor.execute("Select * from phone where name like %%s% order by
name",
(form['name'].value,))

But it blows up...


This should work:

cursor.execute("Select * from phone where name like %s order by name",
('%'+form['name'].value+'%',))

-Kirk McDonald
Jan 28 '06 #9
Perfect again Kirk! Now I will study all this so I actually understand
what is happening..

Thanks!

Fred

Jan 28 '06 #10
Dennis Lee Bieber wrote:
On Sat, 28 Jan 2006 10:14:44 -0800, Kirk McDonald <mo******@suad.org>
declaimed the following in comp.lang.python:
The comma is intentional: the MySQLdb wants the argument(s) as a tuple.

The DB-API wants tuples... But my last perusal of the MySQLdb Python
code showed that it would work with naked singletons...


Ah! So it does. However, I still pass 'em as a tuple as a matter of
course, since it's documented that way. *shrug* (Also, it saves that
many keystrokes if I need to add arguments.)

-Kirk McDonald
Jan 28 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: John R | last post by:
I found this old thread while looking for some type of compression solution myself. Long story short, I contacted Lester and ended up writing an LZO solution just as Mark had mentioned below....
175
by: Sai Hertz And Control Systems | last post by:
Dear all, Their was a huge rore about MySQL recently for something in java functions now theirs one more http://www.mysql.com/doc/en/News-5.0.x.html Does this concern anyone. What I...
8
by: Fred | last post by:
Hello, Our website is currently developed in ASP/Mysql 4. The dedicated servers on which it is currently hosted arrive at saturation. Here is their configuration: - 1 server PIV 2,8Ghz 1GB...
2
by: sathyashrayan | last post by:
dear group, I have been working VC++ for some time. My company assigned me a task for an online dictionary search site similar to the onelook.com which I have to make it in php mysql. Since I...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
1
by: mpar612 | last post by:
Hi everyone, I'm not sure if this is asking too much or not. I am trying to get the following PHP code to work on my website. It utilizes PHP 5, MySQL 4.1 and the PEAR DB module. I am...
4
by: WiseG1rly | last post by:
Hey everyone! Still working on a site I posted for a while ago. Essentially I have a search function that is populated through by a database in mySQL and PHP. I need a few things to help the...
7
by: daveeboi | last post by:
..I have been strugling to get this part of my site working correctly even though I can't see anything wrong with my code. I am trying to search a database and display paged results. But everytime...
1
by: gubbachchi | last post by:
Hi, For my project I have a "add" button, upon clicking it, it will take the user to next page where there will be a text box and list of data displayed below it which is fetched from mysql...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.