简体   繁体   中英

mysqli giving an error when string starts with a number

My php script is supposed to search through a database of clients, it works great and everything, except when the client name in the database starts with a number (the column is formatted Varchar(255)).

if(isset($_GET['search'])) {
    $searchVal = "%" . $_GET['search'] . "%";
}

is how I grab it, and I think it's worth mentioning because the autocomplete function on the other side of the site works just fine, and it's using $_POST to send the data (we wanted to use $_GET so that site visitors can navigate).

    $query = "SELECT DISTINCT clients.client_id,
                              clients.logoFileName,
                              clients.clientName,
                              clients.streetAddress,
                              clients.city,
                              clients.state,
                              clients.zip,
                              clients.latitude,
                              clients.longitude

                         FROM clients

                   INNER JOIN clientData

                           ON clients.client_id = clientData.client_id

                        WHERE clients.clientName LIKE ? AND clients.city LIKE ?

                           OR clientData.clientCategory LIKE ? AND clients.city LIKE ?

                           OR clientData.term LIKE ? AND clients.city LIKE ?";

    //setup
    $stmt->prepare($query);
    $stmt->bind_param('ssssss',$searchVal,$cityVal,$searchVal,$cityVal,$searchVal,$cityVal);

And so this search runs just fine for literally 99% of the searches (we have about 1000 clients listed this applies to only the 10 that start with a number).

So if I was to search for "UPS Store 1840", I can search "UPS", "Store", or "1840" and it will return them. But if I search for "14 Candles", neither "14" or "Candles" return any results.

If I search for the same information in PHPMyAdmin the same results happen (EDIT: well, it gives '0 rows returned', not an outright error). BUT, like I said, the query using $_POST works just fine and uses literally the same code (accesses a similar search, but pulls less data and has a LIMIT).

It's most definitely popping an error in MYSQL, so it's got something to do with the info being sent, which makes me think that sending via $_POST is formatting this differently than the $_GET.

What am I doing wrong, and how can I fix it?

Well, for anyone stumbling across this, I think I found out the error. For whatever reason, doing the INNER JOIN was messing it up. Switched to LEFT JOIN and bam, worked like a charm. Added more data to access, and it's pulling from four tables using INNER, then LEFT LEFT, so it was something to do with INNER JOIN across a bunch of tables. The SELECT DISTINCT must stay, or I get a billion results (tempted to use that to add a 'relevance' option to weight searches).

Also, I have a theory that it had something to do with the collation being latin1_swedish_ci or some such, so I went through and changed all tables to utf8_unicode_ci. So one of those two things...

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