我试图在数据库中过滤搜索结果。
我在我的应用程序中实现了一个条形码扫描仪。现在,当我扫描时,它扫描条形码并发出警报。按钮索引1导致这个NSData调用:
NSString *salesStr = @"http://10.0.0.1:";
salesStr = [salesStr stringByAppendingString:@"8080"];
salesStr = [salesStr stringByAppendingString:@"/barcode.php?password="];
salesStr = [salesStr stringByAppendingString:@"test"];
salesStr = [salesStr stringByAppendingString:@"&db="];
salesStr = [salesStr stringByAppendingString:@"testDB"];
salesStr = [salesStr stringByAppendingString:@"&barNum="];
salesStr = [salesStr stringByAppendingString:self.scannedItem];
NSURL * url = [NSURL URLWithString:salesStr];
NSData * data = [NSData dataWithContentsOfURL:url];10.0.0.1:8080/barcode.php?password=test&db=testDB&barNum=0123456789012
所以,我要做的是:如果扫描的项目不在数据库中,那么数据库的segue使用标识符scanMissing;当然,如果扫描的项在数据库中,那么它就使用scanSuccess。
if(data == nil)
{
[self performSegueWithIdentifier:@"scanMissing" sender:self];
}
else
{
[self performSegueWithIdentifier:@"scanSuccess" sender:self];
}
}我的问题是,我不能使用零,因为有一些字节仍然在传输。设置断点时,我获得数据的下列信息:
数据(_NSInlineData) 2字节
我是否必须将if语句更改为2字节才能使其工作?或者我该怎么做?
编辑: PHP代码:
$host = "localhost";
$db = $_REQUEST['db'];
$user = "root";
$pass = $_REQUEST['password'];
$barcode = $_REQUEST['barNum'];
$connection = mysql_connect($host, $user, $pass);
//Check to see if we can connect to the server
if(!$connection)
{
die("Database server connection failed.");
}
else
{
//Attempt to select the database
$dbconnect = mysql_select_db($db, $connection);
//Check to see if we could select the database
if(!$dbconnect)
{
die("Unable to connect to the specified database!");
}
else
{
$query = "SELECT ITEM_ID, ITEM_Kitchen_Name FROM it_titem WHERE ITEM_ID=$barcode";
$resultset = mysql_query($query, $connection);
$records = array();
//Loop through all our records and add them to our array
while($r = mysql_fetch_assoc($resultset))
{
$records[] = $r;
}
//Output the data as JSON
echo json_encode($records);
}
}
?>发布于 2014-08-19 19:00:21
PHP代码输出一个JSON编码的数组。因此,最小的可能响应是[]。无论如何,它不可能是nil。
您可以尝试将数据与[]进行比较,也可以解码JSON并检查数组的长度。第二个选择是迄今为止最可靠的国际水文学组织。
NSError *error = nil;
NSArray *results = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (error != nil)
{
// There was an error parsing the JSON, do whatever you want
}
else if (results.count == 0)
{
// no results
}
else
{
// results
}(还没有测试)。
发布于 2014-08-19 19:16:13
您应该通过PHP来处理这个问题。
如果你发现了什么
{
"Success" : true,
"Response" : {
"ID": 1234,
"Name":"Name string"
}
}如果不是
{
"Success" : false,
"Response" : {
}
}另外,您也可以尝试使用JSONModel
https://stackoverflow.com/questions/25390271
复制相似问题