[英]PHP and MYSQL: Query returns null on column with values
I have a problem with a simple SQL query: 我有一个简单的SQL查询的问题:
SELECT a.idElemento, b.nombre, b.descripcion
FROM productos_elementos a
INNER JOIN elementos_adicionales b
ON a.idElementoAdicional=b.id_elemento_adicional
INNER JOIN precio_elemento c
ON a.idElemento=c.idElemento
WHERE a.idProducto = 1 AND c.idPolitica = 1
When I execute this query on my database, it returns: 当我在我的数据库上执行此查询时,它返回:
IdElemento Nombre Descripcion
1 p1 p1_desc
2 p2 p2_desc
However my PHP code returns me this values: 但是我的PHP代码返回给我这个值:
IdElemento Nombre Descripcion
1 null null
2 null null
I don't know why that it's happening. 我不知道为什么会这样。 Any ideas? 有任何想法吗? This is the code: 这是代码:
//$query is a string with the querty: SELECT....
$result = $this->conn->query($query);
$aux = $result->fetchAll(PDO::FETCH_OBJ);
$results_to_send = json_encode($aux);
If I print the $variable $results_to_send at this moment, it is initialized to: 如果我此时打印$ variable $ results_to_send,它将被初始化为:
[{"idElemento":"1","nombre":null,"descripcion":null},{"idElemento":"2","nombre":null,"descripcion":null}]
I use the values in this piece of code: 我使用这段代码中的值:
foreach ($results_to_send as $key => $value){
...
echo $value->idElemento //It prints 1 or 2
...
echo $value->nombre //It is null
...}
UPDATE: Finally I have found the problem. 更新:最后我发现了问题。 I don't know how to resolve it yet, but it's a start. 我不知道如何解决它,但这是一个开始。
The problem is the string stored in the database. 问题是存储在数据库中的字符串。 Both columns have characters with accents. 两列都有带重音的字符。 For example the first row is: (1,p1_náme, p1_désc). 例如,第一行是:(1,p1_náme,p1_désc)。 It returns (1, null, null) 它返回(1,null,null)
If I change it for (1,p1_name, p1_desc), it returns (1,p1_name, p1_desc). 如果我将其更改为(1,p1_name,p1_desc),则返回(1,p1_name,p1_desc)。
UDATE 2: Problem solved thanks to Andrew Mackrodt comment ^^ UDATE 2:由于Andrew Mackrodt评论^^,问题解决了
I had the same problem which was caused by non-matching charsets between PHP's MySQLI and MySQL Database. 我有同样的问题,这是由PHP的MySQLI和MySQL数据库之间的非匹配字符集引起的。 Not all charsets have the same characters which can cause PHP to return a null. 并非所有字符集都具有可导致PHP返回null的相同字符。
You can compare and set your charset by locating your database charset (in phpmyadmin or in a query like this ) and set in PHP using $mysqli::set_charset("short_name") ( http://php.net/manual/en/mysqli.set-charset.php ) 您可以通过查找数据库字符集(在phpmyadmin中或在像这样的查询中)来比较和设置您的字符集,并使用$ mysqli :: set_charset(“short_name”)在PHP中设置( http://php.net/manual/en/ mysqli.set-charset.php )
If you'd like to see what charsets are available here's a list. 如果您想查看可用的字符集,请参阅此列表。 http://dev.mysql.com/doc/refman/5.6/en/charset-charsets.html http://dev.mysql.com/doc/refman/5.6/en/charset-charsets.html
For PDO just do: 对于PDO,只需:
$db = new PDO("mysql:host=host.address.com.br; dbname=yourdb; charset=utf8;", "username", "password");
$db->exec("set names utf8");
and don't forgot to define Collation as utf8 too. 并且不要忘记将Collation定义为utf8。
See this answer 看到这个答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.