0

We have a $link variable like so:

$link = mysqli_connect('localhost', 'user', 'pass', 'db');

var_dump($link); returns the correct values for the keys:

["affected_rows"]=>  int(0)
["client_info"]=> string(79) "mysqlnd 5.0.11-dev - ..."
["client_version"]=> int(50011)
...

but json_encode($link) returns all nulls:

"affected_rows": null,
"client_info": null,
"client_version": null,
...

Is there a way to get the json_encoded string to have the same values?

Majid Fouladpour
  • 29,356
  • 21
  • 76
  • 127

2 Answers2

1

From official documentation

Parameters

value

The value being encoded. Can be any type except a resource.

You supplied $link which is resource

From source code it seems that for unsupported types (like resource) it will yield "null" value:

    default:
        encoder->error_code = PHP_JSON_ERROR_UNSUPPORTED_TYPE;
        if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
            smart_str_appendl(buf, "null", 4);
   }
rkosegi
  • 14,165
  • 5
  • 50
  • 83
  • 1
    This is true, but the question is not whether `json_encode` could do the encoding, it can't and I am aware of the fact. The question is: *Is there a way to get the json_encoded string to have the same values?* – Majid Fouladpour Oct 07 '17 at 13:57
0

I was hoping someone had done this and could share the solution. No solution so far, so I'd make an attempt.

As pointed by @rkosegi, $link is a resource. As a result we should expect the following:

var_export will cast the resource to an array (mysqli::__set_state) - so nulls again
foreach also treats it as an array and gives us nulls
var_dump could get the values (but could not be easily captured)
print_r however, gives us the values and could be saved

So, let's use print_r:

$raw = print_r($link, true);

With some string parsing, we could get what we want.

Update - added string parsing with a caveat (error_list):

$link = mysqli_connect('localhost', 'root', 'lilo123', 'm1_s1');
$raw = print_r($link, true);
$raw = explode(PHP_EOL, $raw);
$link_arr = [];
foreach($link as $k => $v) {
  $match = array_filter($raw, function($var) use ($k) { return strpos($var, '['.$k.']'); });
  $val = array_values($match)[0];
  if(strpos($val, ' => ')) {
    $val = explode(' => ', $val)[1];
    $link_arr[$k] = $val;
  }
}
$link_json = json_encode($link_arr, JSON_PRETTY_PRINT);
echo $link_json;

Output:

{
    "affected_rows": "0",
    "client_info": "mysqlnd 5.0.11-dev - 20120503 - $Id: 76b08b24596e12d4553bd41fc93cccd5bac2fe7a $",
    "client_version": "50011",
    "connect_errno": "0",
    "connect_error": "",
    "errno": "0",
    "error": "",
    "error_list": "Array", <-- no good
    "field_count": "0",
    "host_info": "Localhost via UNIX socket",
    "info": "",
    "insert_id": "0",
    "server_info": "5.7.19-0ubuntu0.16.04.1",
    "server_version": "50719",
    "stat": "Uptime: 52396  Threads: 1  Questions: 36754  Slow queries: 0  Opens: 133  Flush tables: 1  Open tables: 60  Queries per second avg: 0.701",
    "sqlstate": "00000",
    "protocol_version": "10",
    "thread_id": "5274",
    "warning_count": "0"
}
Majid Fouladpour
  • 29,356
  • 21
  • 76
  • 127