3

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

6
  • 1
    Really? Here I'm getting 0 results? You should get either Results or No results. Commented Jul 29, 2017 at 10:51
  • @Error404 it's not the problem. Commented Jul 29, 2017 at 10:53
  • 1
    $results= mysqli_query($conn, "SELECT * FROM test WHERE russia = 'привет'") or die(mysqli_error($conn)); please replace this line with mine and see if any error comes ... without an error i am not able to get the issue Commented Jul 29, 2017 at 10:57
  • @Jay What does the following query print in myadmin? SELECT COUNT(*) FROM test WHERE russia = 'привет' Commented Jul 29, 2017 at 11:07
  • You have a column called Russia? Commented Jul 29, 2017 at 11:15

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, you helped me a lot :)
If my post answers your question, please mark it as accepted. Thanks a lot.
-2
  1. You won't get 0 results in any way,you either get results or no results. 2.try removing the quotes from the table name
  2. Check for any Encoding issues with the connection encoding and that the data on the value of column russia is parsed as something else. Try executing the following query before executing your main query

    mysqli_query($conn,"SET character_set_results='utf8',character_set_client='utf8',character_set_connection='utf8',character_set_database='utf8',character_set_server='utf8'");

The problems arise if there are Encoding issues in the connection.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.