简体   繁体   中英

changing from 5.3 to 5.5 php getting SQL Error: 1064 in loop

I have upgraded from PHP 5.3 to 5.5 I used this code:

 foreach($searchArr as $token)
    {


 foreach($longTblDef as $element)
            {



 if (isset($element['th']) && !empty($element['th']))



  $query .= "LOWER(" . $element['td'] . ") 
LIKE '%" . strtolower($token) . "%'";



if (next($longTblDef) && !empty($element['th'])) $query .= " `OR ";                 }`

if (next($searchArr)) $query .= ") AND (";
        }

in 5.3 and it worked fine. Now when I use it in 5.5 I get the message: SQL Error: 1064 -- 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 'LOWER(cases_tbl.host) LIKE '%dawn%') ORDER BY cases_tbl.rec_id DESC' at line 6[]

This is the query:

SELECT
cases_tbl.rec_id AS "cases_tbl.rec_id", 
cases_tbl.date_added AS "cases_tbl.date_added", 
cases_tbl.status AS "cases_tbl.status", 
clients_tbl.client_number AS "clients_tbl.client_number", 
cases_tbl.matter_number AS "cases_tbl.matter_number", 
concat(clients_tbl.client_number, '-', cases_tbl.matter_number) AS "concat(clients_tbl.client_number, '-', cases_tbl.matter_number)", 
cases_tbl.lead_id AS "cases_tbl.lead_id", 
lead_tbl.initials AS "lead_tbl.initials", 
concat(lead_tbl.name_f, ' ', lead_tbl.name_l, ' (', lead_tbl.initials, ')') AS "concat(lead_tbl.name_f, ' ', lead_tbl.name_l, ' (', lead_tbl.initials, ')')", 
cases_tbl.co_lead_id AS "cases_tbl.co_lead_id", 
concat(co_lead_tbl.name_f, ' ', co_lead_tbl.name_l, ' (', co_lead_tbl.initials, ')') AS "concat(co_lead_tbl.name_f, ' ', co_lead_tbl.name_l, ' (', co_lead_tbl.initials, ')')", 
cases_tbl.matter_type_id AS "cases_tbl.matter_type_id", 
matters_tbl.matter_type AS "matters_tbl.matter_type", 
cases_tbl.ref_name AS "cases_tbl.ref_name", 
cases_tbl.client_id AS "cases_tbl.client_id", 
clients_tbl.client_name AS "clients_tbl.client_name", 
cases_tbl.client_ult AS "cases_tbl.client_ult", 
cases_tbl.opposing_lf AS "cases_tbl.opposing_lf", 
cases_tbl.opposing_side AS "cases_tbl.opposing_side", 
cases_tbl.products AS "cases_tbl.products", 
cases_tbl.description AS "cases_tbl.description", 
cases_tbl.rate AS "cases_tbl.rate", 
cases_tbl.servers AS "cases_tbl.servers", 
CONCAT(REPLACE(servers_tbl.server_unc, '/', '/'), LEFT(cases_tbl.ref_name, 1), '/', clients_tbl.client_number, '-', cases_tbl.matter_number, ' ', cases_tbl.ref_name) AS "CONCAT(REPLACE(servers_tbl.server_unc, '/', '/'), LEFT(cases_tbl.ref_name, 1), '/', clients_tbl.client_number, '-', cases_tbl.matter_number, ' ', cases_tbl.ref_name)", 
cases_tbl.tape AS "cases_tbl.tape", 
cases_tbl.date_modified AS "cases_tbl.date_modified", 
cases_tbl.host AS "cases_tbl.host"  

FROM    cases_tbl 
LEFT JOIN clients_tbl on cases_tbl.client_id = clients_tbl.rec_id
LEFT JOIN matters_tbl on cases_tbl.matter_type_id = matters_tbl.rec_id
LEFT JOIN users_tbl as lead_tbl on cases_tbl.lead_id = lead_tbl.rec_id
LEFT JOIN users_tbl as co_lead_tbl on cases_tbl.co_lead_id = co_lead_tbl.rec_id
LEFT JOIN servers_tbl ON cases_tbl.servers = servers_tbl.rec_id

WHERE (

LOWER(cases_tbl.date_added) LIKE '%dawn%' OR 
LOWER(cases_tbl.status) LIKE '%dawn%' OR 
LOWER(concat(clients_tbl.client_number, '-', cases_tbl.matter_number)) LIKE '%dawn%' OR LOWER(concat(lead_tbl.name_f, ' ', lead_tbl.name_l, ' (', lead_tbl.initials, ')')) LIKE '%dawn%' OR 
LOWER(concat(co_lead_tbl.name_f, ' ', co_lead_tbl.name_l, ' (', co_lead_tbl.initials, ')')) LIKE '%dawn%' OR LOWER(matters_tbl.matter_type) LIKE '%dawn%' OR 
LOWER(cases_tbl.ref_name) LIKE '%dawn%' OR 
LOWER(clients_tbl.client_name) LIKE '%dawn%' OR 
LOWER(cases_tbl.client_ult) LIKE '%dawn%' OR 
LOWER(cases_tbl.opposing_lf) LIKE '%dawn%' OR 
LOWER(cases_tbl.opposing_side) LIKE '%dawn%' OR 
LOWER(cases_tbl.products) LIKE '%dawn%' OR LOWER(cases_tbl.description) LIKE '%dawn%' OR 
LOWER(CONCAT(REPLACE(servers_tbl.server_unc, '/', '/'), LEFT(cases_tbl.ref_name, 1), '/', clients_tbl.client_number, '-', cases_tbl.matter_number, ' ', cases_tbl.ref_name)) LIKE '%dawn%' OR 
LOWER(cases_tbl.tape) LIKE '%dawn%' OR 
LOWER(cases_tbl.date_modified) LIKE '%dawn%' OR
LOWER(cases_tbl.host) LIKE '%dawn%') 

ORDER BY cases_tbl.rec_id DESC 

Can someone tell me what I have done wrong here or if there is another way to code the loop that would work in 5.5? Thank you for any assistance you can provide. Dawn

Are you sure you're not coming from PHP 5.2.3 or lower?
Because it looks like starting with PHP 5.2.4, the behaviour of next inside foreach loops has changed.

I tested this snippet at 3v4l :

<?php
$a = array('1', '2', '3', '4', '5');
foreach($a as $b)
{
    var_dump(next($a));
}

PHP 5.2.3 and earlier prints:

string(1) "2"
string(1) "3"
string(1) "4"
string(1) "5"
bool(false)

Whereas PHP 5.2.4-5.6.8 prints:

string(1) "3"
string(1) "4"
string(1) "5"
bool(false)
bool(false)

Instead of returning the next value, it returns the one after the next, which evaluates to false one iteration too early.

Maybe 3v4l is not accurate enough with PHP versions, but if you truly come from PHP 5.3, try running the above snippet and see what it gives you.

I have no idea whether this is intended behaviour or not, but it is most certainly the cause of your problem.

If your $searchArr and $longTblDef arrays have numeric keys, you could use the $key => $value form in foreach and check if the next element exists with isset($longTblDef[$key + 1]) , but a universal solution would be to build an array instead of a string and using implode on it after the loop:

$and = [];
foreach($searchArr as $token)
{
    $or = [];
    foreach($longTblDef as $element)
    {
        if(isset($element['th']) && !empty($element['th']))
        {
            $or[] = "LOWER(".$element['td'].") LIKE '%".strtolower($token)."%'";
        }
    }
    $and[] = implode(' OR ', $or);
}
$query .= implode(') AND (', $and);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM