5

I have a problem with a simple SQL query:

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:

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:

[{"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). It returns (1, null, null)

If I change it for (1,p1_name, p1_desc), it returns (1,p1_name, p1_desc).

UDATE 2: Problem solved thanks to Andrew Mackrodt comment ^^

Rumpelstinsk
  • 2,680
  • 2
  • 19
  • 44
  • post your php code that you are using to fetch the data also. – Code Lღver Feb 13 '14 at 11:32
  • Yeah, you've let out the part where it goes wrong. I bet you are trying to echo the values as array, but you fetch as object. – winkbrace Feb 13 '14 at 11:34
  • Include the code where you're *printing* the results. – alexis Feb 13 '14 at 11:37
  • I have updated the question. Sorry for the inconvenience ^^ – Rumpelstinsk Feb 13 '14 at 11:45
  • 1
    hmm, so nothing wrong with the printing. Are Nombre and Descripcion LOBs or other fields FETCH_OBJ might have trouble with? – winkbrace Feb 13 '14 at 11:53
  • The problem only appears on this fields. Thanks to your answer, I could advance a little. Please see the update ^^ – Rumpelstinsk Feb 13 '14 at 12:35
  • 4
    What collation are you using for these columns? E.g. if you're using `utf8_general_ci` you may need to set the character collation for the connection: `$this->conn->exec('SET NAMES utf8');`. Alternatively, it's better to set the character collation in the connection string `"mysql:host=$host;dbname=$dbname;charset=utf8"`. – Andrew Mackrodt Feb 13 '14 at 12:47
  • Thanks Andrew, That did the trick. I was using utf8_spanish_ci – Rumpelstinsk Feb 13 '14 at 13:05

2 Answers2

2

I had the same problem which was caused by non-matching charsets between PHP's MySQLI and MySQL Database. Not all charsets have the same characters which can cause PHP to return a 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)

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

Community
  • 1
  • 1
Nate Bosscher
  • 341
  • 1
  • 9
-1

For PDO just do:

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

See this answer