Hi All,

I have used eval function before this. But I am not sure why this one is not working for me.

I do have one function which returns me some filter string. It returns string something like this.

AND TM_ID=$row_tMain[TM_ID] AND TM_DATE=$row_tMain[TM_DATE]

I want to use this string in my query something like this.

$sql="SELECT * FROM TABLE WHERE TM_ID > 2000 ".$filterString;

Here $filterString is my string returned from the function as above example.

But when I run the page it gives error that


You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[TM_ID] AND TM_DATE=$row_testMain[TM_DATE]'

My row variable's name is fix as row_tMain but i m unable to evaluate it as value in my query string any idea ?? :(

How can I achieve this without the eval or with eval.

Thanks In Advance
Shariq

Recommended Answers

All 3 Replies

Well as you pointed out (probably without realising it), the array values are not stored in the variable and instead the actual name/code of the array is stored in the variable of your first code box. So try making the sql variable the following:

$sql='SELECT * FROM TABLE WHERE TM_ID > 2000 AND TM_ID='.$row_tMain[TM_ID].' AND TM_DATE='.$row_tMain[TM_DATE];
//or the following two lines:
$filterString='AND TM_ID='.$row_tMain[TM_ID].' AND TM_DATE='.$row_tMain[TM_DATE];
$sql="SELECT * FROM TABLE WHERE TM_ID > 2000 ".$filterString;

Well as you pointed out (probably without realising it), the array values are not stored in the variable and instead the actual name/code of the array is stored in the variable of your first code box. So try making the sql variable the following:

$sql='SELECT * FROM TABLE WHERE TM_ID > 2000 AND TM_ID='.$row_tMain[TM_ID].' AND TM_DATE='.$row_tMain[TM_DATE];
//or the following two lines:
$filterString='AND TM_ID='.$row_tMain[TM_ID].' AND TM_DATE='.$row_tMain[TM_DATE];
$sql="SELECT * FROM TABLE WHERE TM_ID > 2000 ".$filterString;

Thanks for reply,

Here my concern is the $filterString comes from a function in which i have to do some juggling of the string and this resultant string is used in many places. I can not put row_tMain value statically.

I need to add row_tMain 's value dynamically as per my query thats why I was trying with eval.

For clarification
at one time

$filterString's value might be

AND TM_ID=1000 AND TM_DATE=2009-08-10

or

AND TM_ID=2000 AND TM_DATE=2008-08-08

so i can not add row_tMain Directly into the query .

Hope this is clear

Thanks
Shariq

Thanks cwarn,

I have done little change in my code and make the row_tMain variable global and my function was able to put the value directly and no need to eval it.

Hurray :)

Thanks all for your kind reply.
Shariq

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.