gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:48:24 24 4
gpt4 key购买 nike

我的 iOS 应用程序应该使用带参数的字符串和 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);
}
}
}

?>

在您的浏览器中:

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

输出:

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

关于php - 如何使用带参数的字符串从 mySQL 获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28891729/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com