mysql插入语句返回,select语句始终返回php mysql中最后插入的行

When I wrote the select statement it always return the last inserted row in the database. What is the problem, and how can I fix it?

Important NOTE: A friend of mine took the same code and it worked for her properly!

if (isset($_GET["name"])) {

$pid = $_GET['name'];

// get a product from products table

//)or die(mysql_error()

$result = mysql_query("SELECT * FROM food WHERE name = $pid");

//mysql_query($result,$con);

if (!empty($result)) {

// check for empty result

if (mysql_num_rows($result) > 0) {

$result = mysql_fetch_array($result);

$product = array();

$product["name"] = $result["name"];

$product["unit"] = $result["unit"];

$product["calory"] = $result["calory"];

$product["carbohydrate"] = $result["carbohydrate"];

$product["category"] = $result["category"];

// success

$response["success"] = 1;

// user node

$response["product"] = array();

array_push($response["product"], $product);

// echoing JSON response

echo json_encode($response);

} else {

// no product found

$response["success"] = 0;

$response["message"] = "No item found";

// echo no users JSON

echo json_encode($response);

}

} else {

// no product found

$response["success"] = 0;

$response["message"] = "No product found";

// echo no users JSON

echo json_encode($response);

} */

} else {

// required field is missing

$response["success"] = 0;

$response["message"] = "Required field(s) is missing";

// echoing JSON response

echo json_encode($response);

解决方案if (mysql_num_rows($result) > 0) {

$result = mysql_fetch_array($result);

$product = array();

$product["name"] = $result["name"];

$product["unit"] = $result["unit"];

$product["calory"] = $result["calory"];

$product["carbohydrate"] = $result["carbohydrate"];

$product["category"] = $result["category"];

// success

$response["success"] = 1;

// user node

$response["product"] = array();

array_push($response["product"], $product);

// echoing JSON response

echo json_encode($response);

}

replace this with

while(mysql_num_rows($result) > 0 && ($result = mysql_fetch_array($result))) {

$product = array();

$product["name"] = $result["name"];

$product["unit"] = $result["unit"];

$product["calory"] = $result["calory"];

$product["carbohydrate"] = $result["carbohydrate"];

$product["category"] = $result["category"];

// success

$response["success"] = 1;

// user node

$response["product"] = array();

array_push($response["product"], $product);

// echoing JSON response

echo json_encode($response);

}

the result is array and you are not looping through it

so it givesonly one element in the array

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值