Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
select语句始终返回php mysql中最后插入的行_Php_Mysql_Select - Fatal编程技术网

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

select语句始终返回php mysql中最后插入的行,php,mysql,select,Php,Mysql,Select,当我编写select语句时,它总是返回数据库中最后插入的行。问题是什么,我如何解决 重要提示:我的一个朋友使用了相同的代码,它对她正常工作 这是因为mysql\u fetch\u array不在循环中,请将其放入while循环并进行检查 if (mysql_num_rows($result) > 0) { $result = mysql_fetch_array($result); $product = array(); $p

当我编写select语句时,它总是返回数据库中最后插入的行。问题是什么,我如何解决

重要提示:我的一个朋友使用了相同的代码,它对她正常工作


这是因为
mysql\u fetch\u array
不在循环中,请将其放入while循环并进行检查

        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);
    }
替换为

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);
    }
结果是数组,而不是循环通过它
因此它只给数组中的一个元素

简单地将所有元素放在一个循环中并不能解决这个问题。您给出的代码将给出相同的结果。。最后一个

$product需要在循环之前声明,否则,它将始终重置。此外,为了在不覆盖的情况下填充$product数组,需要将其设置为多维

$product[]['name']=$result[“name”]

储存产品的理想方式是这样。。在我看来

$product = array();
while($result = mysql_fetch_array($result)) {    
        $product[$result['id']] = $result;

您可能还需要修复sql注入
yoursite.com?name=bla或1=1
将返回数据库中的所有行您的代码工作得非常好,您是一个天才!!多谢各位
$product = array();
while($result = mysql_fetch_array($result)) {    
        $product[$result['id']] = $result;