如何使用带参数的字符串从 mySQL 获取数据?

How get data from mySQL using string with params?

我有 iOS 应用程序,它应该使用带参数的字符串和 PHP 脚本从 MySQL 数据库接收 JSON 数据。字符串看起来像这样: NSString *getDataURL1 = [NSString stringWithFormat:@"http://myhost.com/jsoncar.php?carOne=%@", _carOneToServer];其中 _carOneToServer 是包含用户在我的应用程序中选择的特定汽车型号的字符串,例如 "AUDI A3 1.8TFSI"。在 MySQL 数据库中,我有完全相同的车型。

PHP 脚本应该检查 ULR 查询和 MySQL 数据库之间的匹配模型以及该车型的 returns 参数。

到目前为止,我写了一些代码,但 returns[]。我知道我需要使用变量,但我不知道 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);

尝试在调用查询时删除连接。例如,

$resultset = mysql_query($query);

而不是:

$resultset = mysql_query($query, $connection);

您有 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);
}
}   
}

?>

在您的浏览器中:

http://localhost/1.php?carOne=AUDI%20A3%201.8TFSI

输出:

[{"carFuelEconomy":"27","carPurchasePrice":"30795"}]