Forum Moderators: coopster & phranque

Message Too Old, No Replies

MySQL Select Statement - Help Needed

A quick question, really need an answer!

         

Eric_Lander

5:14 pm on Oct 20, 2001 (gmt 0)

10+ Year Member



Hello all...

I'm venturing into PHP and MySQL today, on a time-sensitive project. I've managed to do everything I want to do with my database EXCEPT...

I need to select a record set from my table, where a column needs to contain a string of characters.

How would I go about this?

SELECT * from TABLENAME where COLUMN...

I lose it there! Please help me! A search on the various engines yielded little help.

Thanks!
~ Eric

sugarkane

5:26 pm on Oct 20, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



SELECT * from TABLENAME where COLUMN like "%stringtomatch%";

...is probably the simplest way (the %s are wildcards)

More complex searches can be done using

SELECT * from TABLENAME where COLUMN regexp "regular_expression";

Eric_Lander

5:38 pm on Oct 20, 2001 (gmt 0)

10+ Year Member



mysql(constables_mbca_org, "select * from directory where (services LIKE "%Notary%")");

That is exactly what I am using... I need to get all records from the database that have the string "Notary" in the column called "services"

But it is not working at all... MySQL is returning an error on the next line. Any ideas?

sugarkane

5:44 pm on Oct 20, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Replace the "s round the sql query with 's, or escape the ones around %Notary%, and lose the brackets - ie

mysql(constables_mbca_org, 'select * from directory where services LIKE "%Notary%"')

Eric_Lander

5:52 pm on Oct 20, 2001 (gmt 0)

10+ Year Member



SugarKane...

You are the best. Thank you! It works like a charm!

Thank you SOOOO much!

TheLynxEffect

2:52 am on Oct 22, 2001 (gmt 0)



I'll actually offer a quick explaination as to why sugarkane's fix worked if I may....

basically, you are generally only allowed to have 1 pair of " " in a single line of PHP. The PHP 'interpreter' reads the 2 " " and then kind of thinks Ok that's the end of that. When you open another set of " " it gets very confused.

This is a really dumbed down version of what happens - hey, for me to understand it, it HAS to be simple - but I hope you get the general Jist of things. In future, just try replacing " with ' like sugarkane said and it might work.....does for me half the time!

Rich

sugarkane

5:36 pm on Oct 22, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's about on the money, LynxEffect.

One issue to bear in mind with " and ' - true in both Perl and PHP - is that any variables within " " will be evaluated, while within ' ' the variable name will be printed as-is.
[perl]
$foo="bar";
print "$foo"; # output: bar
print '$foo'; # output: $foo
[/perl]