1

From what I can tell, PHP fails on building array with special character. Not sure where or why this is happening and how to solve.

function findRelated($subpass){
$subpass = "$subpass%";
global $mysqli;
if($stmt = $mysqli->prepare("SELECT pass FROM `passwords` WHERE pass LIKE ? LIMIT 500"))

        {

            $stmt->bind_param('s', $subpass);
            if(!$stmt->execute())
            {
                $stmt->close();
                return array("_status" => 3, "_msg" => "An error occured when executing your request");
            }
            if(!$result = $stmt->get_result())
            {
                return array("_status" => 3, "_msg" => "An error occured when executing your request");
            }
            while ($row = $result->fetch_assoc())
            {
                $rows[] = $row;
            }
            if(!empty($rows))
            {
                $stmt->close();
                return array("_status" => 1, "_msg" => $rows);
            }
            else
            {
                $stmt->close();
                return array("_status" => 2, "_msg" => "Your password was not found in the database.");
            }
        }

        /* Close connection */
        $stmt->close();
        return array("_status" => 3, "_msg" => "Something is wrong with database connection");
    }

This table has passwords that look like this: 'asdf', 'asdf 123', 'ASDF 12345', 'ASDF JKL ö'.

The last password: 'ASDF JKL ö' breaks something, most likely at the while loop or stmt->get_result, but it does not trigger an error. I just get an empty result back. Which means the while loop must have run at least once.

I've tried removing the 'ö' from the row, and it works like a charm. Changing 'ASDF JKL ö' to 'ASDF JKL '

I run the function with e.g. $result = findRelated('asdf');

Database has collation: latin1_swedish_ci. PHP and apache on Ubuntu runs with default config.

Thank you

connery
  • 13
  • 3

1 Answers1

0

Try setting the charset on the mysqli object to the same as your database. It could be UTF8, and you would set it like this:

$mysqli->set_charset('utf8');

To figure out the appropriate charset, see this question: How do I see what character set a MySQL database / table / column is?

Community
  • 1
  • 1
Chris Dale
  • 2,222
  • 2
  • 26
  • 39