mysql查询只选择第一条记录

时间:2015-11-01 10:57:53

标签: php mysql sql

我正在尝试执行查询以更新php中的记录。我的PHP代码是这样的:

/**
 * Gets the MediaStore video ID of a given file on external storage
 * @param filePath The path (on external storage) of the file to resolve the ID of
 * @param contentResolver The content resolver to use to perform the query.
 * @return the video ID as a long
 */
private long getImageIdFromFilePath(String filePath,
    ContentResolver contentResolver) {


    long imageId;
    Log.d(TAG,"Loading file " + filePath);

            // This returns us content://media/external/images/media (or something like that)
            // I pass in "external" because that's the MediaStore's name for the external
            // storage on my device (the other possibility is "internal")

    Uri imagesUri = MediaStore.Images.getContentUri("external");

    Log.d(TAG,"imagesUri = " + imagessUri.toString());

    String[] projection = {MediaStore.Images.ImageColumns._ID};

    // TODO This will break if we have no matching item in the MediaStore.
    Cursor cursor = contentResolver.query(imagesUri, projection, MediaStore.Images.ImageColumns.DATA + " LIKE ?", new String[] { filePath }, null);
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(projection[0]);
    imageId = cursor.getLong(columnIndex);

    Log.d(TAG,"Image ID is " + imageId);
    cursor.close();
    return imageId;
}

我首先在我的第一个选择查询中获得与一个订单相关的所有餐点ID然后我尝试使用while循环更新每个餐的状态但是我得到的第一餐记录更新而不是另一个。我得到这个的回复:

$get_meals_info =  "Select meal_id from orders WHERE order_id ='$order_id'";
$result = mysql_query($get_meals_info) or die(mysql_error());;

if(mysql_num_rows($result)!=0)
{
    while($meal_id = mysql_fetch_assoc($result)) {
        $id=    $meal_id['meal_id'];
        //$update_status="Select meal_id from orders where order_id=" . $meal_id['order_id'] . "";

        $update_status =  "update order_main SET STATUS='$order_status' WHERE id =" . $meal_id['meal_id'] . "";
        $result = mysql_query($update_status);
        //$meal_id=null;
        echo $id;
    }

2 个答案:

答案 0 :(得分:4)

您为数据提取和更新使用相同的变量$result,第一个update会导致您丢失原始数据,然后$result仅包含true {1}}而不是资源。

答案 1 :(得分:1)

为什么要使用循环?您可以使用单个查询完成此操作。例如:

update order_main
    SET STATUS = '$order_status'
    WHERE id IN (Select meal_id from orders WHERE order_id = '$order_id');

另请注意,您应该使用mysqli_ functions并参数化查询,这样就不会直接传递用户输入。