Php 如何使用JSON删除表中的记录
我有一个JSON文件,它将数据引入iOS。我还想看看是否有超过2条记录 如果有超过2条记录,我希望它删除除最后2条记录以外的所有记录 如何将其插入到这个php文件中Php 如何使用JSON删除表中的记录,php,sql,json,Php,Sql,Json,我有一个JSON文件,它将数据引入iOS。我还想看看是否有超过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."); }
$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);
}
}
?>
您使用的是限制1,这意味着您只能返回1条记录。你打算增加限额吗 如果需要剔除返回的记录数组,则始终可以使用PHP count()函数和array_slice()函数
是的,该限制仅适用于一个函数,我需要另一个值用于其他函数。我需要删除这些记录,因为它们一旦被使用就不会再被使用。所以我需要一些东西,比如如果记录>2,然后删除除最后2之外的所有内容。是否有一行代码可以执行此操作