【问题标题】:How to delete records in a table with JSON如何使用 JSON 删除表中的记录
【发布时间】:2013-12-20 20:32:56
【问题描述】:

我有一个将数据带入 iOS 的 JSON 文件。我也想让它看看是否有超过 2 条记录。

如果有超过 2 条记录,我希望它删除除最后 2 条记录之外的所有内容

如何将它插入到这个 php 文件中

$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
    {
        $query = "SELECT * FROM test ORDER BY id DESC LIMIT 1";
        $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);
    }
}
?>

【问题讨论】:

    标签: php sql json


    【解决方案1】:

    您使用的是 LIMIT 1,这意味着您只能获得 1 条记录。您是否打算提高限制?

    如果您需要剔除返回的记录数组,您总是可以使用 PHP count() 函数和 array_slice() 函数...

    http://php.net/array_slice

    【讨论】:

    • 是的,该限制仅适用于一个功能,我需要其他值用于其他功能。我需要删除记录,因为一旦使用它们就不会再次使用。所以我需要类似 if record > 2 然后删除除最后 2 之外的所有内容。id 有一行代码来执行此操作
    猜你喜欢
    • 2019-06-19
    • 1970-01-01
    • 2011-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 2013-10-27
    • 1970-01-01
    相关资源
    最近更新 更多