Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL query working in phpmyadmin but not in php

Tags:

php

mysql

I want to select data from a table in MySQL. My code in php:

$conn = mysqli_connect($db_server, $db_benutzer, $db_passwort, $db_name);

$results= mysqli_query($conn, "SELECT * FROM `test` WHERE russia = 'привет'");
if(mysqli_num_rows($results) > 0) { 
    echo "Results";
}
else {
    echo "No results";
}

mysqli_close($conn);

Here I'm getting "No results". But when I run the SELECT-code directly in phpmyadmin i get a result.

What's wrong? Thank you

like image 365
JAYJKB Avatar asked Mar 22 '26 02:03

JAYJKB


1 Answers

You have cyrillic characters in your query, so it may be necessary to set mySQL connection encoding. If you are using utf-8, insert following line after mysqli_connect:

mysqli_query($conn, "SET NAMES 'utf8'");

Or if your script is saved in windows-1251, use the following: mysqli_query($conn, "SET NAMES 'cp1251'");

For more information about connection character sets and encodings please see the manual

And why does the query work in phpMyAdmin? Because it probably sets encoding for you in the background.

like image 176
Jirka Hrazdil Avatar answered Mar 23 '26 14:03

Jirka Hrazdil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!