0

I'm can't encode entire MYSQLI object properly. all return NULL. I've tried many methods to convert encoding character but no luck.

$sql = "SELECT * FROM t_user";
$char = $mysqli->set_charset('utf8mb4');
var_dump($char);

$mysqli->query("SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci");
$mysqli->set_charset('utf8mb4');

//Source: https://gist.github.com/oscar-broman/3653399
utf8_encode_deep($mysqli);

$result = $mysqli->query($sql);
var_dump($mysqli);

var_dump(get_object_vars($mysqli));

echo "\n\n\n".json_encode($mysqli);

echo "\n\n\n".$result->field_count;

echo("\n\n\n".json_last_error_msg());

function copied from: https://gist.github.com/oscar-broman/3653399

function utf8_encode_deep(&$input) {
    if (is_string($input)) {
        $input = utf8_encode($input);
    } else if (is_array($input)) {
        foreach ($input as &$value) {
            utf8_encode_deep($value);
        }

        unset($value);
    } else if (is_object($input)) {
        $vars = array_keys(get_object_vars($input));

        foreach ($vars as $var) {
            utf8_encode_deep($input->$var);
        }
    }
}

Output :

var_dump($char);

D:\xampp\htdocs\mb\action.php:636:
bool(true)

var_dump($mysqli);

D:\xampp\htdocs\mb\action.php:644:
class mysqli#1 (19) {
  public $affected_rows =>
  int(24)
  public $client_info =>
  string(79) "mysqlnd 5.0.12-dev - 20150407 - $Id: 38fea24f2847fa7519001be390c98ae0acafe387 $"
  public $client_version =>
  int(50012)
  public $connect_errno =>
  int(0)
  public $connect_error =>
  NULL
  public $errno =>
  int(0)
  public $error =>
  string(0) ""
  public $error_list =>
  array(0) {
  }
  public $field_count =>
  int(9)
  public $host_info =>
  string(20) "localhost via TCP/IP"
  public $info =>
  NULL
  public $insert_id =>
  int(0)
  public $server_info =>
  string(21) "5.5.5-10.1.38-MariaDB"
  public $server_version =>
  int(50505)
  public $stat =>
  string(135) "Uptime: 72296  Threads: 1  Questions: 5062  Slow queries: 0  Opens: 64  Flush tables: 1  Open tables: 51  Queries per second avg: 0.070"
  public $sqlstate =>
  string(5) "00000"
  public $protocol_version =>
  int(10)
  public $thread_id =>
  int(643)
  public $warning_count =>
  int(0)
}

var_dump(get_object_vars($mysqli));

D:\xampp\htdocs\mb\action.php:646:
array(19) {
  'affected_rows' =>
  NULL
  'client_info' =>
  NULL
  'client_version' =>
  NULL
  'connect_errno' =>
  NULL
  'connect_error' =>
  NULL
  'errno' =>
  NULL
  'error' =>
  NULL
  'error_list' =>
  NULL
  'field_count' =>
  NULL
  'host_info' =>
  NULL
  'info' =>
  NULL
  'insert_id' =>
  NULL
  'server_info' =>
  NULL
  'server_version' =>
  NULL
  'stat' =>
  NULL
  'sqlstate' =>
  NULL
  'protocol_version' =>
  NULL
  'thread_id' =>
  NULL
  'warning_count' =>
  NULL
}

json_encode($mysqli);

{"affected_rows":null,"client_info":null,"client_version":null,"connect_errno":null,"connect_error":null,"errno":null,"error":null,"error_list":null,"field_count":null,"host_info":null,"info":null,"insert_id":null,"server_info":null,"server_version":null,"stat":null,"sqlstate":null,"protocol_version":null,"thread_id":null,"warning_count":null}

$result->field_count;

9

json_last_error_msg()

No error
elliotching
  • 972
  • 1
  • 10
  • 33
  • You forgot something. Namely, *why* do you want to json encode a mysqli object. I am asking because there is not a single reason to do so – Your Common Sense May 05 '19 at 10:34
  • to simplify my PHP code so that I don't have to manually check it everytime if this query valid or contain error? I've to write so many if else to create an array with $r['error'] in PHP, and then in Android, once again check same thing again 'error' exist (feeling redundant). I know this sounds very lazy person but I wish to be faster. Another reason is affected_rows – elliotching May 05 '19 at 10:46
  • FYI, I've simplified my question to give an example for MYSQLI only, instead I want both MYSQLI and MYSQLI_RESULT, and both having same problem. – elliotching May 05 '19 at 10:51
  • What i meant bout a lot of if else is because of mysqli result can be returned as false instead of object so i cannot access a property in this situation. Therefore, i will end up a nested if else – elliotching May 05 '19 at 11:26
  • And what are you going to do in the else clause? – Your Common Sense May 05 '19 at 13:30
  • Still I don't get it. To get affected_rows you just need to access this property. Why encode it? – Your Common Sense May 05 '19 at 13:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/192874/discussion-between-elliotching-and-your-common-sense). – elliotching May 05 '19 at 23:58
  • that duplicated question just completely unrelated to my question – elliotching May 06 '19 at 00:34
  • can I have at least a reason why encode mysqli directly is inappropriate? – elliotching May 06 '19 at 00:40
  • This dupe question is 100% related to the reason why you want encode a mysqi object. That's enough. – Your Common Sense May 06 '19 at 05:10

0 Answers0