简体   繁体   中英

PHP MYSQL mysqli_fetch_array() no results (foreign characters)

I have a bizarre one. I run mysqli_fetch_array all the time and I've never seen it give back no results before when I can echo out the SQL then insert it into PHPMYADMIN and get a result set back. Is it relevant that the results are in Japanese, is that what is causing the issue? Any help appreciated, code below:

$sqli_peekaboo = "SELECT KanjiElementKEB FROM stedict WHERE KanjiElementKEB =      '" . $line1word . "'";
echo $sqli_peekaboo . '<br>';
$result_sqli_peekaboo = mysqli_query($conn, $sqli_peekaboo);
while($row_peekaboo = mysqli_fetch_array($result_sqli_peekaboo))
{
echo '<br>Result:'.$row_peekaboo['KanjiElementKEB'].'<br>';
}

Try to change the encoding before echoing the result, like this:

header('content-type: text/html; charset=utf-8');

Or, if it is a HTML page then in head tags give:

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

You can use like this, give it a try

 SELECT CONVERT(KanjiElementKEB USING utf8 )
 FROM stedict WHERE KanjiElementKEB =      '" . $line1word . "'";

Maybe that play $line1word has something to do with it, as it is bound to be Japanese too. We don't see where $line1word is coming from, but assuming it's unfiltered, try this:

$sqli_peekaboo = "SELECT KanjiElementKEB FROM stedict WHERE KanjiElementKEB = '" . mysqli_real_escape_string($line1word) . "'";

Also, may I ask why you are selecting only what you have already?

while($row_peekaboo = mysqli_fetch_array($result_sqli_peekaboo))
{
if(mb_detect_encoding($row_peekaboo['KanjiElementKEB'])=="UTF-8")
{
echo '<br>Result:'.utf8_decode($row_peekaboo['KanjiElementKEB']).'<br>';
 }
}

Thanks for all the tips and suggestions. With your help I managed to fix it: I changed the connection encryption, I've not done that before.

$conn = mysqli_connect("localhost", "******", "******", "stedict") or mysqli_connect_error(); $conn->set_charset("utf8");

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