Reputation:
How can I have the php script return json without the square brackets? I am also getting the INT values back with double quotes for some reason. Is it possible to remove those as well?
What I am getting now:
[{"id":"1","name":"Jack","username":"Jack1","age":"23"}]
What I would like:
{"id": 1,"name":"Jack","username":"Jack1","age":23}
The following is the php script that returns the JSON:
<?php
$con = mysqli_connect("localhost", "username", "password", "database");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This SQL statement selects ALL from the table 'Equipment'
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT username, name FROM DriverInfo WHERE (username = '$username') and password = '$password' ";
// Check if there are results
if ($result = mysqli_query($con, $sql)) {
// Create temporary connection
$resultArray = array();
$tempArray = array();
// Look through each row
while ($row = $result->fetch_object()) {
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);
}
mysqli_close($con);
Upvotes: 0
Views: 1576
Reputation: 7
Finally, encode the array to JSON and output the results
echo json_encode(array("data"=>$resultArray));
output:
data:[{"id":1,"name":"Jack","username":"Jack1","age":23}]
Upvotes: 0
Reputation: 18416
The square brackets indicate that your variable originated from an array of objects, and is expected for translating it back to the original value.
However, you can use a combination of JSON_*
constants for the options argument, such as JSON_FORCE_OBJECT | JSON_NUMERIC_CHECK
, to ensure your desired result from json_encode
.
JSON_NUMERIC_CHECK
will remove the double quotes from the numeric
values in your JSON string. JSON_FORCE_OBJECT
will convert all
arrays into objects.To remove the square brackets, if your $resultArray
variable is an array of objects, you will need to determine which value you want. Such as $resultArray[0]
or end($resultArray)
or reset($resultArray)
Example: https://3v4l.org/ip6GN
echo json_encode(reset($resultArray), JSON_FORCE_OBJECT | JSON_NUMERIC_CHECK);
Result:
{"id":1,"name":"Jack","username":"Jack1","age":23}
However in this instance you are looking for a specific record in the database. Assuming there is only one, I suggest updating your query to leverage a prepared statement, to avoid SQL injection attacks.
Additionally I highly recommend hashing your passwords using the built in password_hash
functions.
$sql = 'SELECT `id`, `username`, `name`, `age`
FROM `DriverInfo`
WHERE `username` = ?
AND `password` = ?
LIMIT 1';
$stmt = mysqli_stmt_init($con);
if (mysqli_stmt_prepare($stmt, $sql)) {
mysqli_stmt_bind_param($stmt, 'ss', $username, $password);
$username = $_POST['username'];
$password = $_POST['password'];
mysqli_stmt_execute($stmt);
if ($result = mysqli_stmt_get_result($stmt)) {
if ($row = mysqli_fetch_object($result)) {
echo json_encode($row, JSON_FORCE_OBJECT | JSON_NUMERIC_CHECK);
}
}
}
mysqli_stmt_close($stmt);
mysqli_close($con);
Upvotes: 2
Reputation: 861
You are building an array of the resulting rows from your mysql query. The square brackets are there because that is the notation for an array in JSON. It looks like you only want to return the first row, so you don't need that while statement:
$row = $result->fetch_object();
if ($row) {
echo json_encode($row);
} else {
echo '{}';
}
Also you'll want to be aware of this: How can I prevent SQL injection in PHP?
Upvotes: 1
Reputation: 356
The json you want is in a single element php array, so try doing echo json_encode($resultArray[0])
. Also if you're expecting an int back and you know where, just use parseInt()
in your javascript when handling the response
Upvotes: 0