Newbe - MySQL syntax problem? --- SOLVED ---

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
AJP
Posts: 2
Joined: Fri Jun 03, 2011 4:44 pm

Newbe - MySQL syntax problem? --- SOLVED ---

Post by AJP » Fri Jun 03, 2011 4:46 pm

Hi All, new to LiveCode...looks great so far!

I'm having an issue with the 'WHERE' clause in MySQL. I've looked through the dictionary and forum but cannot find a solution.

If I use this to pull out a specific record from the db all works as expected.

--------------

put "SELECT user FROM auth WHERE user='Jason' " into tSQL

//query the database ( format ready for table)

put revDataFromQuery(tab, cr, gConnectionID, tSQL) into tData

--------------

The problem is when I want to use a variable for the WHERE clause as below;

--------------

put "Jason" into tUsername

put "SELECT user FROM auth WHERE user='tUsername' " into tSQL

//query the database ( format ready for table)

put revDataFromQuery(tab, cr, gConnectionID, tSQL) into tData

--------------

This results in no data?

So the question is: How do I use a variable in the WHERE clause?

Thanks

Andy
Last edited by AJP on Fri Jun 03, 2011 5:00 pm, edited 1 time in total.

dglass
Posts: 519
Joined: Thu Sep 24, 2009 9:10 pm
Contact:

Re: Newbe - MySQL syntax problem?

Post by dglass » Fri Jun 03, 2011 4:54 pm

You have to get your variable out of your string literal SQL statement.

put "SELECT user FROM auth WHERE user='" & tUsername & "'" into tSQL

So that's <equal sign><single quote><double quote> & tUsername & <double quote><single quote><double quote>

That's one way.

Another way is to use a parameter:

put "SELECT user FROM auth WHERE user=:1" into tSQL

Then use the format of DBQuery that accepts the parameters and variables.
Last edited by dglass on Fri Jun 03, 2011 4:59 pm, edited 1 time in total.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Newbe - MySQL syntax problem?

Post by Mark » Fri Jun 03, 2011 4:55 pm

Hi Andy,

Your approach would work in PHP but not in LiveCode :-) One of several possible correct versions of your syntax would be:

Code: Select all

put "SELECT user FROM auth WHERE user='" & tuserName & "' into tSQL"
mind the quotation marks: single_quote double_quote & variable_name & double_quote single_quote

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

AJP
Posts: 2
Joined: Fri Jun 03, 2011 4:44 pm

Re: Newbe - MySQL syntax problem?

Post by AJP » Fri Jun 03, 2011 4:59 pm

Wow quick reply and solution!

Thanks dglass and Mark.

Andy

Post Reply