1

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

2
  • you want output as json string ? Commented May 4, 2016 at 5:39
  • no at the end i need an JSON array for my JS script to creat all the tickets. Commented May 4, 2016 at 5:44

2 Answers 2

1
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();
Sign up to request clarification or add additional context in comments.

1 Comment

Please consider adding details in your answer along with the code snippet, as to what you are doing and why you are doing it.
0

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();    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.