简体   繁体   中英

MySQL Data to JSON Array with inserting a string

This is my Code for now but it only works until the for loop in the mySQLResultsetToJSON() .

<?php    
$servername = "127.0.0.1";
$username = "root";
$password = "123456";
$table = "ticketing";
$link = new mysqli($servername, $username, $password, $table);
if ($link->connect_error) {
    die("Connection failed: " . $link->connect_error);
}
    echo "Connected successfully";    

$result = $link->query("SELECT * from Ticket");

print_r($result);
$final_result = mySQLResultsetToJSON($result);
print_r($final_result);    
$link->close();    

function mySQLResultsetToJSON($resultSet)
{
        for($i = 0; sizeof($resultSet); $i++)
        {
            $rows = array();
            while($r = mysqli_fetch_assoc($resultSet[$i])) {
                $rows[] = $r;
            }
            $jsonResult[$i] = json_encode(array('Results' => $rows));
        }    
        print_r($jsonResult);    
        return $jsonResult;
    }    
?>

Thank you!

Thomas

echo "mysql data<br />";
$result = $link->query("SELECT * from users");
print_r($result->fetch_object());

echo "<br />";

echo "in json<br />";
$res = ['Results' => $result->fetch_object() ];
echo json_encode($res);
$link->close();

User like

$result = $link->query("SELECT * from Ticket");
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
    $rows[] = $r;
}
print "<pre>";
print_r(json_encode(array('Results' =>$rows)));    
$link->close();    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM