将图像信息从服务器转换为json

Help needed pals, i want to convert the information about the image retrieved from a database into json format. Here is the code that gets the information of the image from the database

// Query for a list of all existing files
$sql = 'SELECT `id`, `name`, `mime`, `size`, `created` FROM `file`';
$result = $dbLink->query($sql);

// Check if it was successfull
if($result) {
// Make sure there are some files in there
if($result->num_rows == 0) {
    echo '<p>There are no files in the database</p>';
}
else {
    // Print the top of a table
    echo '<table width="100%">
            <tr>
                <td><b>Name</b></td>
                <td><b>Mime</b></td>
                <td><b>Size (bytes)</b></td>
                <td><b>Created</b></td>
                <td><b>&nbsp;</b></td>
            </tr>';

    // Print each file
    while($row = $result->fetch_assoc()) {
        echo "
            <tr>
                <td>{$row['name']}</td>
                <td>{$row['mime']}</td>
                <td>{$row['size']}</td>
                <td>{$row['created']}</td>
                <td><a href='get_file.php?id={$row['id']}'>Download</a></td>
            </tr>";
    }

    // Close table
    echo '</table>';
}

// Free the result
$result->free();
}
else
{
echo 'Error! SQL query failed:';
echo "<pre>{$dbLink->error}</pre>";
}

Here is what am trying to achieve

{
"success": 1,
"message": "images Available",
"images": [
    {
        "Name": "richmahnn",
        "Mime": "ajh544k",
        "Size": "222",
        "Created": "232014",
        "url": "http://localhost/imageUpload/ajh544k.jpg"
    },
    {
        "Name": "john",
        "Mime": "ajh5644k",
        "Size": "15",
        "Created": "232014",
        "url": "http://localhost/imageUpload/ajh5644k.jpg"
    },

so far this is what i get enter image description here

  1. Create a php array with the desired property layout you want in JSON.
  2. json_encode($yourarraydata);
  3. ????
  4. #Win

From the required JSON data you say you want then this seems like the simplest solution.

Basically create a new class using stdClass() that satisfies your requirements that the JSON data be an object. Then change the while loop to return the result rows as an object rather than an assoc array, this can then be dropped into your new class's images array property, one for each row returned.

Now you have all the data in a PHP class all that is necessary is to convert it to a JSON string using json_encode(). The result of that can be echo'd out as the returned data.

I assume that the class you are using to encapsulate your database access code has a method that will return rows in an object form as well as as an assoc array.

// Query for a list of all existing files
$sql = 'SELECT `id`, `name`, `mime`, `size`, `created` FROM `file`';
$result = $dbLink->query($sql);

$toJson = new stdClass();

// Check if it was successfull
if($result) {
    // Make sure there are some files in there
    if($result->num_rows == 0) {
        echo '<p>There are no files in the database</p>';
        toJson->success = 0;
        toJson->message = 'There are no files in the database';

    } else {
        // Print the top of a table
        echo '<table width="100%">
              <tr>
                <td><b>Name</b></td>
                <td><b>Mime</b></td>
                <td><b>Size (bytes)</b></td>
                <td><b>Created</b></td>
                <td><b>&nbsp;</b></td>
              </tr>';

        // Print each file
        while($row = $result->fetch_object()) {
            echo "<tr>
                  <td>{$row->name}</td>
                  <td>{$row->mime}</td>
                  <td>{$row->size}</td>
                  <td>{$row->created}</td>
                  <td><a href='get_file.php?id={$row->id}'>Download</a></td>
                  </tr>";

            // amend and existing properties
            $row->name = 'http://' 
                         . $_SERVER['HTTP_HOST'] 
                         . '/imageUpload/' 
                         . $row->name;

            // or alternatively add a new property
            $row->url  = 'http://' 
                         . $_SERVER['HTTP_HOST'] 
                         . '/imageUpload/' 
                         . $row->name;

            $toJson->images[] = $row;
        }

        // Close table
        echo '</table>';
        $toJson->message = 'images Available';
        $toJson->imageCount = $result->num_rows;
    }

    // Free the result
    $result->free();
} else {
    toJson->success = 0;
    toJson->message ='Error! SQL query failed:';
    echo 'Error! SQL query failed:';
    echo "<pre>{$dbLink->error}</pre>";
}

If you are calling this with an AJAX call from your page, then to return the data just do

echo json_encode($toJson);
exit;

If you are doing something else with the resulting json string, then just do this

$json_string = json_encode($toJson);