Database Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesDatabase Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old December 4th, 2003, 03:34 PM
bighimot bighimot is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 4 bighimot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Another simple SELECT problem

Hallo to all, I'm strugling with a (supposed) stupid problem with a SELCT query on a MySQL database.

System info are:
MySQL 3.23.41 on a RH 7.2
PHP 4.0.6

My problem is that I'm trying to execute a query like:

php Code:
Original - php Code
  1.  
  2. $query = "SELECT * FROM my_table WHERE field_1 = $to_find";


but what I get is just the list of the fields on my table without any record.
I have tried also in the form some of you suggested in a similar post, i.e.:

php Code:
Original - php Code
  1.  
  2. $query = "select * from my_table where field_1 ='".$to_find."'";


but then in the page which was supposed to show the good output I get only an error:

Error: You have an error in your SQL syntax near ''ptsc'' at line 1


where 'ptsc' is the string I want to find and the field $field_1 is defined as varchar(10). Actually what I have is a table with some codes like 'ptsc' and I want to get all the records with this code.

Eventually, in the future I'd like to combin it with a logical AND with another field, but for now I can't get work the first part of it.

Do you have any suggestion?

Thanks,
Bighimot

Reply With Quote
  #2  
Old December 4th, 2003, 04:10 PM
nazly nazly is offline
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Colombo,SriLanka
Posts: 1,325 nazly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 18 sec
Reputation Power: 3
Send a message via Yahoo to nazly
RE: Another simple SELECT problem

Does $to_find contains 'ptsc' with the single quote or did you use single quotes to show us it as a string??
If it has single quotes try using it as like this..

php Code:
Original - php Code
  1.  
  2. $query = "select * from my_table where field_1 ='".addslashes($to_find)."'";


Reply With Quote
  #3  
Old December 4th, 2003, 07:21 PM
brut brut is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 367 brut User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 30 sec
Reputation Power: 2
RE: Another simple SELECT problem

Try
php Code:
Original - php Code
  1.  
  2. echo $to_find;

It sounds like your variable has no value.

Reply With Quote
  #4  
Old December 5th, 2003, 06:58 AM
bighimot bighimot is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 4 bighimot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Another simple SELECT problem

Thank you for your suggestions but still it does not work.

$to_find has a value which is ptsc, which it isn't single quoted.

What it looks weird to me is that it seems that querying with a string value will not work.

I've tried using a similar query like:
php Code:
Original - php Code
  1.  
  2. $query = "SELECT * FROM my_table WHERE field_2 = $to_find_2";


in this case field_2 is a numeric field (int unsigned) and the var $to_find_2 contains a numeric value. This one works.
Again if a use a field_3 which is defined as date (formatted as YYYY-MM-DD), it still doesn't work.

In the MySQL shell I would use (and actually works):

SELECT * FROM my_table WHERE field_2 = "ptsc";

but quoting seems not allowed via PHP because any attempts to add quoting result in that error.

Am I forgetting some stupid thing?

Bighimot

Reply With Quote
  #5  
Old December 5th, 2003, 08:37 AM
nazly nazly is offline
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Colombo,SriLanka
Posts: 1,325 nazly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 18 sec
Reputation Power: 3
Send a message via Yahoo to nazly
RE: Another simple SELECT problem

Try using it like this..

php Code:
Original - php Code
  1.  
  2. $query = "SELECT * FROM my_table WHERE field_2 = '$to_find_2'";


If you don't enquote the value with a single quote it assumes it as a fieldname. Hope I'm correct..

Reply With Quote
  #6  
Old December 5th, 2003, 09:52 AM
bighimot bighimot is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 4 bighimot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: RE: Another simple SELECT problem

You wrote:

Quote:
Try using it like this..

php Code:
Original - php Code
  1.  
  2. $query = "SELECT * FROM my_table WHERE field_2 = '$to_find_2'";


If you don't enquote the value with a single quote it assumes it as a fieldname. Hope I'm correct..


Well, it's more or less the same. I get:

Error: You have an error in your SQL syntax near ''' at line 1

If I do not use quotes it does not work, if I do quoting it throws an error.
It looks like a catch, but I don't understand why.

Thank you anyway Nazly.

B.

Reply With Quote
  #7  
Old December 5th, 2003, 10:09 AM
bighimot bighimot is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 4 bighimot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Another simple SELECT problem

Got it!!!!!!!

Thank you all for your help. All the suggestions I got drove me to the solution.

Well it's a funny stupid thing. PHP passing the query to another script automatically puts slashes in front of my data, thus I just needed to use stripslashes() and all works fine.

Thank you all, again

Bighimot.

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesDatabase Help > Another simple SELECT problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT