我有iOS App,它应该使用带参数的字符串和PHP脚本从MySQL数据库接收JSON数据。字符串看起来像这样:NSString * getDataURL1 = [NSString stringWithFormat:@“ http://myhost.com/jsoncar.php?carOne=%@”,_carOneToServer ];其中_carOneToServer是字符串,其中包含用户在我的应用程序中选择的特定汽车型号,例如“ AUDI A3 1.8TFSI”。在MySQL数据库中,我有完全相同的汽车模型。
PHP脚本应检查ULR查询和MySQL数据库之间的匹配模型,并返回此汽车模型的参数。
到目前为止,我已经编写了一些代码,但是它返回[]。我知道我需要使用变量,但是我不了解PHP,因此需要您的帮助。谢谢你。
PHP脚本:
$host = "localhost"; //Your database host server
$db = "MyDataBase"; //Your database name
$user = "user"; //Your database user
$pass = "password"; //Your password
$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 {
if (isset($_GET['carOne'])) {
$carModel = $_GET[carOne];
$query = "SELECT carFuelEconomy, carPurchasePrice FROM cars
WHERE carModel = $carModel";
$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);
}
}
}
MySQL数据库:
CREATE TABLE cars
(
id INT PRIMARY KEY NOT NULL auto_increment,
carModel VARCHAR(255),
carFuelEconomy int,
carPurchasePrice int,
distanceTraveledDaily int,
lenghtOfOwnership int
);
-- Insert data into our table
INSERT INTO cars(carModel, carFuelEconomy, carPurchasePrice, distanceTraveledDaily, lenghtOfOwnership)
VALUE ('AUDI A3 1.8TFSI', 27, 30795, 30, 76);
INSERT INTO cars(carModel, carFuelEconomy, carPurchasePrice, distanceTraveledDaily, lenghtOfOwnership)
VALUE ('AUDI A3 2.0TDI', 36, 33495, 30, 76);
INSERT INTO cars(carModel, carFuelEconomy, carPurchasePrice, distanceTraveledDaily, lenghtOfOwnership)
VALUE ('AUDI A3 2.0TFSI', 33, 34095, 30, 76);
您有2个错误,请对此进行更改:
$carModel = $_GET[carOne];
$query = "SELECT carFuelEconomy, carPurchasePrice FROM cars
WHERE carModel = $carModel";
为了这:
$carModel = $_GET['carOne'];
$query = "SELECT carFuelEconomy, carPurchasePrice FROM cars
WHERE carModel = '$carModel'";
编辑。代码完成(1.php):
<?php
$host = "localhost"; //Your database host server
$db = "YourDataBase"; //Your database name
$user = "YourUser"; //Your database user
$pass = "YourPass"; //Your password
$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 {
if (isset($_GET['carOne'])) {
$carModel = $_GET['carOne'];
$query = "SELECT carFuelEconomy, carPurchasePrice FROM cars
WHERE carModel = '$carModel'";
$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);
}
}
}
?>
在浏览器中:
输出:
[{"carFuelEconomy":"27","carPurchasePrice":"30795"}]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句