简体   繁体   中英

PHP UTF-8 search not working

For whatever weird reason I can't search non-English characters. The script dies. It dies at search, I think. Blank page, half of layout, nothing after PHP script.

Code was copied from a working site (my own), minus layout, so it should work. But it doesn't.

On search.php:

<meta charset='utf-8' />

On form:

<form action='search.php' method='get' accept-charset='utf-8'>

On db_connect.php:

mysqli_set_charset($db_connect, "utf8");

On database: utf8_general_ci

What else am I missing?

search.php (results page):

<?php

include ("php/form.php");

$searchTerm = trim ($_GET['keyname']);

if (!empty ($searchTerm)) {
    if (strlen ($searchTerm) > 2) {
        include "php/search.php";
        } else {
            echo "<h1>Enter 3 or More Characters</h1><hr />$search_form";
            };
    } else {
        echo "<h1>Enter Search Term</h1><hr />$search_form>";
        };

?>

php/search.php:

$query = "SELECT * FROM members WHERE first_name LIKE '%$searchTerm%' OR
                                      last_name LIKE '%$searchTerm%' OR
                                      date LIKE '%$searchTerm%'";

$results = mysqli_query ($db_connect, $query)
           or die (mysqli_error (db_connect));

if (mysqli_num_rows ($results) >= 1) {
    $record = "";
    while ($row = mysqli_fetch_array ($results)) {
        include "result.php";
        } echo $record . "<hr />$search_form</main>";
    } else {
        echo "<h1>0 Matching Records</h1><hr />$search_form";
        };

Errors:

Illegal mix of collations for operation 'like'

Your code is a mess - or you copy/pasted in a hurry with errors.

First your query:

$query = " SELECT * FROM members WHERE first_name LIKE '%".mysqli_real_escape_string($db_connect, $searchTerm)."%' ";

Mind the mysqli_real_escape_string - the minimum sanitize you need.

Then the result:

$results = mysqli_query ($db_connect, $query) or die (mysqli_error ($db_connect));

You have messed mysqli_ and mysql_ (in die function). Not good.

Now, if these two changes don't help you, try to put at the very top of your script, above everything else:

error_reporting(E_ALL);
ini_set('display_errors', '1');

This should help you pinpoint any php errors in your code.

Found the problem. Need a fix...

date LIKE '%$searchTerm%' isn't working. Breaks the code. So far, I've tried changing LIKE to = , removing % , putting in mysqli_real_escape_string ($db_connect , $searchTerm) .

As for database field, it's a date type.

Illegal mix of collations for operation 'like'

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