MySQL query working in phpmyadmin but not in php - php

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

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.

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
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.

Related

Urdu / Arabic font data from MySQL is displaying as ????? in JSON

I am developing an Android app with Urdu/Arabic data store in MySQL database on my web server and using JSON_Encoding to generate the JSON string. The JSON string is then being used in Android app to perform various functions (populating RecyclerView and other view objects with data). I am able to store Urdu / Arabic data in MySQL database, but when I use PHP script to generate JSON, all the fields containing Urdu characters is displaying data as ??????
I was using the utf8mb4_unicode_ci as I read the this is easy for storing non-English data and performing multiple functions, but after this encoding problem, I have changed that to utf8_general_ci for all the tables and fields in MySQL database. Below is the PHP script I am using to generate the JSON string from MySQL:
<?php
require "conn.php";
mysqli_query("SET NAMES 'utf8'");
mysqli_query('SET CHARACTER SET utf8');
$sql_qry = "SELECT * FROM countrybasic;";
$result = mysqli_query($conn, $sql_qry);
$response = array();
while($row = mysqli_fetch_array($result)){
array_push($response, array("id"=>$row[0],"name"=>$row[1],"capital"=>$row[2],"continent"=>$row[3],"population"=>$row[4],"gdp"=>$row[5]));
}
echo json_encode(array("server_response"=>$response));
mysqli_close($conn);
?>
The Name and Capital fields are the ones I store my Urdu data in.
Please help me out to resolve this issue.
Thanks.
Create your table [countrybasic] with collation utf8mb4_unicode_ci and make the name column with the same colation.
Now insert some sample data in different languages.
Get the data using MySQLi query result.
Note: If you save the data when the collation is different and get that one after changing the collation then that data will not fetch correctly.
I hope this will work.
You just have to change the Charset to UTF8, and you can use these lines for PHP to do it:
$statSQL= 'SET CHARACTER SET utf8';
mysqli_query($your_db,$sSQL)
or die ('charset in DB didn\'t change');
I hope this help :)
$CONNECTION = mysqli_connect($host,$user,$password,$database);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query ($CONNECTION ,"set character_set_results='utf8'");
$queryutf8 = "select * from yourtable";
$res_utf8 = mysqli_query($CONNECTION ,$queryutf8 );
You need to change the default utf8 in the wamp server
check the below link for more detail.
Arabic characters doesn't show in phpMyAdmin

SQL Server Strange Characters from PHP but not Manual Query

I have php code to update data to sql server using below statement
UPDATE [table] SET [third]='Several times (2–5x)' WHERE ...
However the updated data in sql server shows strange characters Several times (2–5x), but if I tried to execute the SQL statement in Management Studio, it doesn't give me strange characters.
Here is my PHP code:
$sql = "UPDATE [table] SET [third]='Several times (2–5x)' WHERE ..."
$sql_update_user_result = odbc_exec($connection, $sql);
What am I doing wrong?
Looks like an encoding problem to me.
Try querying "SET NAMES utf8" before executing the update query.
odbc_exec($connection, "SET NAMES utf8");
$sql = "UPDATE [table] SET [third]='Several times (2–5x)' WHERE ..."
$sql_update_user_result = odbc_exec($connection, $sql);
The hyphen in 2–5x is not the ascii minus but another similar looking puncuation mark (u+2013).
Just delete the – and type -.

What could break charsets between database and display?

I am using a MySQL database to store some strings which contain German umlauts (äüö). The table "testtable" and the column "text" are utf8_bin collated, and PHPMyAdmin tells me the "MySQL connection" is also utf8_bin.
I then use a PHP script to read the strings and display them:
$sql = "SELECT `text` FROM `testtable` WHERE `id`=$id";
$db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, false);
mysql_select_db(MYSQL_DATABASE,$db);
$result = mysql_query($sql,$db);
if (!$result or mysql_errno()) die("Something was wrong with the query: $sql");
$rows = array();
while(($rows[] = mysql_fetch_assoc($result)) || array_pop($rows));
foreach ($rows as $r) {echo $r['text']}
The file itself is encoded in utf-8 according to my editor, and the page is declared as utf-8 in the head:
<meta charset="utf-8">
Yet, the text "This is a testmessage äüöß" in the database is displayed as "This is a testmessage ����". When I write umlauts directly into the HTML of the PHP file, or echo umlauts directly, they are displayed correctly, so I figure the encoding mistake must be somewhere between database and PHP server.
What factors that I've overlooked could be messing up the encoding here, or what could I try to figure out where exactly the problem lies?
PHPMyAdmin tells you only about its own connection charset and not about connection between database and your application. Try to run query
SET NAMES utf8
right after you made a connection

Issue when inserting UTF8 characters in db from script

I have a small script that receive some data through GET and inserts data in a db. My problem is when sending some UTF-8 characters. The scritp receives them ok but inserts them in a weird way. I printed the query in my page, executed it with phpmyadmin and works ok that way. So, my problem is when executing the query through my script (it doesn't work if I execute a constant query with those characters). Does sending characters by post resolve the issue?
Thank you
Try this:
mysql_set_charset('utf8');
Your entire setup has to be UTF-8. That means your web page, PHP, the database connection, AND the database tables, all have to be in UTF-8 mode. Given it works in the admin pages and not via script, I'm guessing it's your database connection. Try doing a set names='utf-8' before doing your insert query, and see if that fixes things. If it does, then your db connection is using some OTHER character set and mangling your text as it goes from PHP->database.
Add this in the php file before your query:
mysql_query('SET NAMES utf8');
For example:
//DB connection
$con = mysql_connect('localhost', 'root', '');//db details
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydatabase", $con);//DB name
mysql_query('SET NAMES utf8');
//query
$sql="SELECT * FROM users WHERE uid=".$user_id;// your query
$result=mysql_query($sql);

How can I get UTF8 to work correctly using mysql and php?

I have a database where the table is encoded as utf8. I have a value in it that's in Korean. The characters display fine in the database. But when they are echoed from the database I get a bunch of question marks.
Here is my code, after the connect and select_db statements:
mysql_query('SET NAMES utf8');
$query = 'SELECT * FROM english WHERE id = ' . $_GET['dealerID'];
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
What am I doing wrong here? (Yes, 'english' is the right table). I tried Michael's suggestion below to encode the table as utf8_general_ci and I get a MySQL error. Suggestions? What's the correct name of the character set?
If I run a query in PHPMyAdmin, I get 서울시 서초구 서초1동 1425-10 세중프라자 4층/
Review this guide which talks about UTF8, mysql, and PHP all together.
Summary:
make sure the browser knows the page is in utf8
<?php header("Content-type: text/html; charset=utf-8"); ?>
The table in mysql needs to be set for utf8 as well as the string fields within the db
utf8_general_ci
tell php that when it talks to mysql to talk in utf8
mysqli_set_charset('utf8');
Here are some more details as well.

Categories