-1
我的當前數據庫由位類型對象組成,以表示布爾變量。 如何在Swift中表示MySQL位的類型3
但是,當我試圖從MySQL獲取這些數據到Swift 3時,它返回Nil。我知道我的PHP文件工作正常,因爲當我單獨運行我的PHP代碼時,它會打印出正確的一組值。
PHP文件
<?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);
?>
當我註釋掉狀態變量,它打印出正確的價值觀。所以我認爲它返回nil是因爲Bool不是在MySQL中表示Bit類型的正確方法。我試過Int,NSInteger,但都沒有工作。所以我真的不知道什麼類型實際代表狀態(位類型)。 Swift文件
/*
* 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()
}
PHP代碼是從數據庫讀取的,Swift是解析JSON的,它們不是等價的。你的問題可能來自json解釋,而不是來自數據庫的'Bit'值。 – Cristik
是的,我明白了,那麼我應該如何表示jsonElement [「狀態」]然後 –
然後請更新問題,然後在它的當前形式是誤導 – Cristik