473,399 Members | 3,038 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,399 software developers and data experts.

oddball MySQL query


Hi everyone,

What I'm trying to do is take php variables i got from user input, and
pass them as the MySQL query terms.

$query = "select * from ident where ".$searchtype1."=".$searchterm1."";

ERROR: Invalid query: You have an error in your SQL syntax near '=' at
line 1

I have also tried,

$query = "select * from ident where ".$searchtype1." like
'%".$searchterm1."%'";

ERROR: Invalid query: You have an error in your SQL syntax near 'like
'%%'' at line 1

both return similar errors as you can see. I have o idea if the problem
lies with the variable (i try echo-ing, and printing them and i get
nothing) or if it actually is with my sql syntax (i enter the first query
above in mysql from the shell without variable obviously and it returns
the results perfectly). Here is the rest of my code and any help is
greatly appreciated.

Form for getting variables:

<form action="searchrsiident.php" method="post" align="center">
Choose Search Type:<br>
<select name="searchtype1">
<option value="NSN">NSN
<option value="NIIN">NIIN
<option value="NOMENCLATURE">NOMENCLATURE <option value="CAGE">CAGE
<option value="Field5">Field5
<option value="REV">REV
<option value="EQUIP">EQUIP
<option value="RSI_P/N">RSI_P/N
<option value="WAITING_IN">WAITING_IN <option value="NOTES">NOTES
<option value="SOS_CODE">SOS_CODE
<option value="WORK_ORDER">WORK_ORDER <option value="L_QUOTE">L_QUOTE
<option value="A_QTY">A_QTY
<option value="A_PRICE">A_PRICE
<option value="L_WO">L_WO
<option value="L_NOTE">L_NOTE
</select>
<br>
Enter value to search by:<br>
<input name="searchterm1" type=text>
<br>
<input type=submit value="Search">
</form>
Relevent part of my php code (i hope):

$searchtype1 = addslashes($searchtype1); $searchterm1 =
addslashes($searchterm1);

$db = mysql_connect("localhost", "browseuser", "");

if (!$db)
{
echo "Error: Could not connect to database. Please try again
later."; exit;
}
}
mysql_select_db("rsi_ident");
$query = "select * from ident where ".$searchtype1." like
'%".$searchterm1."%'";

$result = mysql_query($query, $db) or die("Invalid query:
".mysql_error()); $num_results = mysql_num_rows($result);
Thanks again

Joe
Jul 16 '05 #1
3 5372
On Wed, 16 Jul 2003 08:33:37 -0500, joemyre wrote:

Hi everyone,

What I'm trying to do is take php variables i got from user input, and
pass them as the MySQL query terms.

$query = "select * from ident where ".$searchtype1."=".$searchterm1."";

ERROR: Invalid query: You have an error in your SQL syntax near '=' at
line 1

I have also tried,

$query = "select * from ident where ".$searchtype1." like
'%".$searchterm1."%'";

ERROR: Invalid query: You have an error in your SQL syntax near 'like
'%%'' at line 1
<snip!>
Thanks again

Joe


i haven't read your post thouroughly, but here is my suggestion. First
off, how are you debugging this? I find it very useful to print to the
screen the actual SQL query being sent to the server and test it inside a
MySQL client session if I am having problems. Have you done this?

Try this. Before you pass the query to the MySQL server (using
mysql_query()), echo it to the screen, cut, and paste it into a MySQL
prompt:

$query = "select * from ident where ".$searchtype1." like '%".$searchterm1."%'";
echo "<pre>\n$query\n</pre>";
exit();

highlight, copy, and paste into mysql prompt.

does the query work?
is the query as printed in the browser quite what you expected it would
be?

later...

--
Jeffrey D. Silverman | jeffrey AT jhu DOT edu
Johns Hopkins University | Baltimore, MD
Website | http://www.wse.jhu.edu/newtnotes/

Jul 16 '05 #2
joemyre wrote:
$query = "select * from ident where ".$searchtype1." like
'%".$searchterm1."%'";
echo "<pre>\n$query\n</pre>";
exit();

highlight, copy, and paste into mysql prompt.

does the query work?
is the query as printed in the browser quite what you expected it would
be?


Ok I tried printing the query to screen and there is definatly an error
with the variables. The query printed to the screen doesnt even have
them! almost time to start ripping my hair out. thanks for that great
debugging tip Jeffrey.


You might want to try $_POST['searchterm1'] or $_GET['searchterm1'] and the
same for all your other variables. Read PHP for info on global variables.

Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 16 '05 #3
$searchtype1=$_POST['searchtype1'];
$searchterm1=$_POST['searchterm1'];

that worked for me. Al was right on with the global variables hint.
Everything works great now, thanks.

joe
Jul 16 '05 #4

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

Similar topics

0
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
1
by: Saqib Ali | last post by:
I have created 2 tables in my MySQL database. A_TAB and B_TAB. They have auto-incrementing integer primary keys respectively named A_ID & B_ID. When I created B_TAB, I declared a field named A_ID...
1
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer...
1
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work....
3
by: Juan Antonio Villa | last post by:
Hello, I'm having a problem replicating a simple database using the binary log replication, here is the problem: When the master sends an update to the slave, an example update reads as follows:...
1
by: Ike | last post by:
Recently, I began using a different MySQL verver (i.e. different machine as well as different version#, going from 4.12a to 4.1.9 max). The following query used to work: select firstname,...
3
by: Me Alone | last post by:
Hello: I am trying to edit some C code I found in "The definitive guide to using, programming, and administering MySQL" by Paul DuBois. This C client program connects and then segfaults when...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.