Insert Enters/Spaces of Text in PhpMyAdmin - php

<?php
require ('sql_connect.php');
$query = "select * from `products`";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$imagem = $row['imagem'];
$texto = $row['texto'];
echo "<img src=Images/Products/$imagem> <br> $texto";
}
?>
I have the image and the text of the image that i want to show in my database.
I show the image but i have problems with the text... The text dont show the ENTERS/SPACES. There are some ways to resolve this?
Image of problem: http://imgur.com/wOGo5I5,oUxMcaS
I want this: http://imgur.com/wOGo5I5,oUxMcaS#1
Database: utf8_general_ci
In database text is saving with enters/spaces...

maybe something is wrong on the encoding of connection. in that sql_connect.php file, after you connect, please execute this one:
mysql_query("SET NAMES 'utf8'");
if this doesn't help, try this one:
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conn);
where $conn is your connection. This will make sure everything is UTF-8
Also please make sure the column on your tables are utf8_general_ci
After all this, please re-enter your data to your db and try again.

Related

Mysql select giving the wrong answer inside php

I have this issue with a PHP script when querying a mysql DB.
The query is:
SELECT COUNT(*) FROM theTable WHERE fieldValue = ‘Dom-Rémy'
I have checked that when performing by hand this query in the administration console (phpMyAdmin) I can find one record matching the request, which is what I expect.
But inside a PHP script where the code is as follows, I always get 0 records (instead of 1):
$Query = "SELECT COUNT(*) FROM theTable WHERE fieldValue = 'Dom-Rémy'";
$DBR = mysql_query($Query,$Connection);
$NBR = mysql_result($DBR,0,0);
printf("NBR: %d\n",$NBR);
Why is that? I suspect the european character inside the fieldValue is what causes the problem, but what should I do to make it work?
Using something like this before Your query to set charset of client and results:
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conn);
Use only count count(*) wont work some time
$query = "SELECT COUNT FROM theTable WHERE fieldValue = 'Dom-Rémy'";
$result = mysql_query($query);

Php mysql query charset

I have problem with query charset for mysql bd in php script.
I'm placing GET parameter inside select query and it work good for all Latin characters, but with Cyrillic characters it returns me empty table. If I place some value with Cyrillic in query instead of GET parameter, query works as I want. Likewise, I get result query inside php-Admin and it works.
All Cyrillic characters in result shows me correct.
I've checked everything: php-file is utf8, GET parameter value is utf8, mysql_client_encoding() returns me utf8.
I've tried all I found - nothing helps me.
header('Content-type:text/html; charset=utf-8');
mysql_set_charset('utf8', $link);
mysql_query("SET NAMES utf8", $link);
mysql_query("SET CHARACTER SET 'utf8'", $link);
mysql_query("SET SESSION collation_connection = 'utf8_general_ci'", $link);
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $link);
All of this DOESN'T HELP.
Here is my php script.
<?php
$link = mysql_connect('localhost', 'r96036lg_searche', 'Jh1ZT4]^');
//mysql_set_charset('utf8', $link);
//mysql_query("SET NAMES utf8", $link);
//mysql_query("SET CHARACTER SET 'utf8'", $link);
//mysql_query("SET SESSION collation_connection = 'utf8_general_ci'", $link);
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $link);
//echo mysql_client_encoding($link);
mysql_select_db('r96036lg_searche', $link);
//mysqli::set_charset('utf8');
$query = "select id, name, typeid from MainObjects where used = 1";
if(isset($_GET['name']) && !empty($_GET['name'])) {
//$name = mb_convert_encoding($_GET['name'], "UTF-8", "win1251");
$name = $_GET['name'];
//echo mb_detect_encoding($name, "auto", false);
//$name = iconv(mb_detect_encoding($name, "auto", false), "UTF-8", $name);
//echo $name;
$query .= " and typeid = 1 and (name like '%".$name.
"%' or id in (select parentid from MainObjects where name like '%"
.$name."%' and used = 1))";
}
//echo $query;
$result = mysql_query($query, $link);
if(!$result) echo mysql_error();
else {
$out = "[";
while($row = mysql_fetch_row($result)) {
... //here is myoutput
}
$out .= "]";
echo $out;
}
?>
This is how you should do it. It will not solve the caracter encoding thing, but it will help with the reste. (And perhaps even solve your problem, because it might just be related to not escaping the values).
$query = "select id, name, typeid from MainObjects where used = 1";
if(isset($_GET['name']) && !empty($_GET['name'])) {
$name = mysql_real_escape_string($_GET['name']);
$query .= " and typeid = 1 and (name like '%".$name.
"%' or id in (select parentid from MainObjects where name like '%"
.$name."%' and used = 1))";
}
//echo $query;
$result = mysql_query($query, $link);
I this does not help there is a character coding mismatch somewhere. There are three points:
GET data: did you try to encode the url like example.com/index.php?name=%d0%ba (cyrillic small letter ka)
communication with server. If you did really try all you said, there should not be an error
data in the tables: Does the collation of the table really correspond to its content. What I might suspect is that there is a mismatch. If the data is in UTF-8, but the table isn't, mysql will try to convert the data from one character set into another, and as a result it will not much.

How to insert ö/ä/ü in Database using php utf8

It was asked a dozen times, I know, but the only thing I want is to insert a German letter like 'Ä' into a database using a php script which simply doesn't work using the existing solutions.
<?php
header('Content-type: text/html; charset=utf-8');
$con = mysqli_connect("localhost", "username", "password");
mysqli_set_charset($con, "utf8mb4");
mysqli_select_db($con, "database");
mysqli_query($con, "SET SESSION CHARACTER_SET_RESULTS =utf8mb4;");
mysqli_query($con, "SET SESSION CHARACTER_SET_CLIENT =utf8mb4;");
mysqli_query($con, "SET NAMES 'utf8mb4';");
mysqli_query($con,
"SET character_set_results = 'utf8',
character_set_client = 'utf8',
character_set_connection = 'utf8',
character_set_database = 'utf8',
character_set_server = 'utf8';");
$title = 'Ö';
$result = mysqli_query($con,
"INSERT INTO Test (Title)
VALUES ('" . utf8_encode($title) . "');");
#Doesn't work, this was inserted: Ã
?>
The charset of the column 'Title' is set to utf8mb4_general_ci and of the Table itself its utf8_general_ci.
MySQL Server version is 5.5.31
MySQL Charset is UTF-8 Unicode
Does anybody know what I am doing wrong or am I missing something here?
As Mihai said, I had to remove the utf8_encode() function, which messed the charset up. After removing it, it works as smooth as it should.

Mysql insert encoding issue

I am trying to insert Utf-8 string in a cell with utf8_general_ci collation
My code:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
...
print mb_detect_encoding($name);
$query = "INSERT INTO items SET name='$name', type='$type'";
print $query;
mysql_set_charset('utf8'); // also doesn't help
$result0 = mysql_query("SET CHARACTER SET utf8") or die(mysql_error()); // I've added this line, but it doesn't solve the issue
$result01 = mysql_query('SET NAMES utf8') or die("set character ".mysql_error()); // still nothing
$result = mysql_query($query) or die(mysql_error());
What I see in the html output:
UTF-8 INSERT INTO waypoints SET name='ČAS', type='ts'
What I see in the database, as name in the inserted row:
?AS
Now my string is utf-8, my table and cell is utf-8 .. why the wrong encoding?
Ok guys, thank you all for help, it looks like I've solved the issue with:
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $link);
right after creating the connection
(especially #Bartdude for the article, which gave me a hint)

Insert chinese character to mysql db using PHP

I would like to ask why the code shown below, cannot insert the chinese character $split 5 to the MySQL db. I have created a db using utf8_general_ci and table as well.
$mysqli = new mysqli($GLOBALS["mysql_host"], $GLOBALS["mysql_user"], $GLOBALS["mysql_passwd"], $GLOBALS["mysql_database"]);
$stmt = mysql_query("SET character_set_results 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8");
$stmt = $mysqli->prepare("INSERT INTO cell (`add`) VALUES (?)");
$stmt->bind_param("s", $split[5]);
$stmt->execute();
$stmt->close();
$mysqli->close();
First, there is a typo. You've used mysql_query() instead of mysqli_query()
Following the mysqli documentation in PHP, mysqli_set_cahrset() is the way to go:
$mysqli->set_charset ('utf8');
So I would add the line above and remove the mysql_query('SET ...'); statement

Categories