Mysql returning null improperly - MYSQLI PHP

Asked

Viewed 700 times

3

I’m trying to make a SELECT through PHP mysqli, but it’s returning null in all parameters of the object returned by $mysqli->query($sql).

O objeto que retornou após o método query

Even so it returns the rows I selected from the table, only some values have come null also:

Linha retornada pelo SELECT

Follows my code:

$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$cell_number = $_POST['cell'];

$sql = sprintf("SELECT * FROM SMS WHERE Number LIKE '%%%s%%'", $cell_number);
$query = $mysqli->query($sql) or die($mysqli->error.__LINE__);


while($row = $query->fetch_assoc()) {
    $result[] = $row;
}

die(json_encode(array('resultado' => $result, 'debug' => $query, 'sql' => $sql)));

I tried to see if it could be some Mysql syntax error, but I played the query directly in the database and it worked normally. I had the same problem with another code I was doing yesterday and I couldn’t fix it. It’s weird, considering I always do it the same way and it works...

Where am I wrong? Someone can help me?

Thank you.

  • Yes, return in JSON format to use in Ajax.

  • I think that’s right, Guilherme. I’m bringing the result in PHP array instead of JSON and it’s coming back right. Do you know why this might be happening? I’ve always used the conversion of arrays to JSON and never had this problem.

  • Edited. I put the last two indexes only for debug purpose.

  • Only one doubt, in the table the fields EmailCounterpart, Provider, Balance and Direction are blank?

  • The EmailCounterpart, Provider and Direction usually comes in white even. But the Balance and the Message that are coming null in JSON always comes filled.

  • William, I appreciate your help. As our colleague Bruno said below, the problem was that I was sending a data that is not in UTF-8 to json_encode, so it was not working. I managed to solve :)

  • Good, I wish you success in your project

Show 2 more comments

1 answer

4


Once again the great villains of history are the charset and the "achism" of PHP.

json_encode() accepts strings only when encoded in UTF-8. If you encounter any during the coding process, PHP will take that value as NULL, quietly, instead of giving you a touch with some Notice, for example.

You can fix this by changing the encoding of your data before you inform the array to json_encode(). Two easy ways to achieve this is with iconv or mb_convert_encoding().

For example, assuming your information in the bank is stored with charset latin1, you would do that:

while( $row = $query->fetch_assoc() ) {

    $result[] = array_map(

        function( $string ) {

            return iconv( 'iso-8859-1', 'utf-8', $string );

        }, $row
    );
}

Here was used an anonymous function in the mold of a Closure, available from PHP 5.3. I hope you’re using at least this version, but in any case you can do:

while( $row = $query->fetch_assoc() ) {

    $result[] = array_map( 'cb', $row );
}

// ...

function cb( $string ) {
    return iconv( "iso-8859-1", "utf-8", $string );
}

The more laborious but more appropriate alternative would be to create a new database, already in UTF-8 and re-insert all data also in UTF-8.

Of course you wouldn’t have to do it manually, you could create a script that would do it for you, but that’s another five hundred.

Adapted of that answer in the Soen.

  • Thank you so much, Bruno! I did not do exactly the same. My bank is in UTF-8, but the strings I was sending no. I made a array_map('utf8_encode', $row) before giving json_encode and it worked!

  • It’s good too. I didn’t mention why relying on utf8_encode() always smelled like gambiarra.

  • I get it. I’ll try to do it the way you showed it above. Yesterday I couldn’t, but I’ll do it better. Thanks.

  • It’s basically the same thing you were able to do, only if the server has been configured by chimpanzee, ICONV may have been manually disabled. Already the functions multibyte (mb_*) require an external library that may not be enabled on the server.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.