Meine aktuelle Datenbank besteht aus einem Bit-Objekt, um eine boolesche Variable darzustellen.Wie man den MySQL-Bit-Typ in Swift 3 darstellt
Wenn ich jedoch versuchte, diese Daten von MySQL zu Swift 3 zu greifen, gibt es Nil zurück. Ich weiß, dass meine PHP-Datei gut funktioniert, denn wenn ich meinen PHP-Code alleine ausführe, druckt er die korrekte Menge von Werten aus.
PHP Datei
<?php
session_start();
$current_username = $_SESSION["username"];
$current_password = $_SESSION["password"];
// open database
$con = mysqli_connect("localhost","root","root","fridge_items");
$mysqli = new mysqli("localhost","root", "root", "fridge_items");
// Check connection
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
//Getting the user_id
$query_id = "SELECT id FROM user WHERE username='$current_username'";
$result_id = $mysqli->query($query_id);
/* associative array */
//Fetches the user id
$row_id = $result_id->fetch_array(MYSQLI_ASSOC);
$user_id = $row_id["id"];
//Fetches the user id
$sql = "SELECT * FROM items where user_id='$user_id'";
// Check if there are results
if ($result = mysqli_query($con, $sql)) {
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
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);
}
//Close connections
mysqli_close($con);
?>
Als ich aus den Statusvariablen kommentiert, druckt er die richtigen Werte aus. Also denke ich, dass es Nil zurückgibt, weil Bool nicht der richtige Weg ist, den Bit-Typ in MySQL darzustellen. Ich habe versucht Int, NSInteger, aber keiner von ihnen funktioniert. Ich bin mir also nicht sicher, welcher Typ tatsächlich den Status repräsentiert (Bit Type). Swift Datei
/*
* Fetching items from database
*/
func fetchItems() -> Void {
let url: String = "http://localhost/fridge_app/fetchItems.php" //this will be changed to the path where service.php lives
//created NSURL
let requestURL = NSURL(string: url)
//creating NSMutableURLRequest
let request = URLRequest(url: requestURL! as URL)
//creating a task to send the post request
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
//exiting if there is some error
if error != nil {
print("error is \(error)")
return;
}
// Grabbing the items
var jsonResult: NSMutableArray = NSMutableArray()
do {
jsonResult = try JSONSerialization.jsonObject(with: data!, options:JSONSerialization.ReadingOptions.mutableContainers) as! NSMutableArray
} catch let error as NSError {
print(error)
}
var jsonElement: NSDictionary = NSDictionary()
let items: NSMutableArray = NSMutableArray()
for i in 0 ... jsonResult.count-1
{
jsonElement = jsonResult[i] as! NSDictionary
let item = itemModel()
print(jsonElement["status"])
//the following insures none of the JsonElement values are nil through optional binding
if let name = jsonElement["name"] as? String,
let status = jsonElement["status"] as? Bool
/*let date_in = jsonElement["date_in"] as? NSDate,
let count = jsonElement["count"] as? NSInteger,
let image = jsonElement["image"] as? NSObjectFileImage,
let expiration_date = jsonElement["expiration_date"] as? NSDate,
let item_id = jsonElement["item_id"] as? NSInteger*/
{
item.name = name
item.status = status
/*item.date_in = date_in
item.count = count
item.image = image
item.expiration_date = expiration_date
item.item_id = item_id*/
}
items.add(item)
print(item)
}
}
//executing the task
task.resume()
}
Der PHP-Code liest aus der DB, der Swift analysiert einen JSON, sie sind nicht äquivalent. Ihr Problem stammt wahrscheinlich von der JSON-Interpretation, nicht vom 'Bit'-Wert aus der Datenbank. – Cristik
Ja, das verstehe ich, also wie soll ich jsonElement ["status"] dann –
Dann dann aktualisieren Sie die Frage dann, in seiner aktuellen Form ist irreführend – Cristik