Select and Update in same query

前端 未结 3 1339
野的像风
野的像风 2021-01-13 05:49

Can you select and update in the same query? I\'m trying to count views to create a top blog posts by views for my sidebar.

$sql = mysql_query(\"SELECT * FRO         


        
3条回答
  •  伪装坚强ぢ
    2021-01-13 06:31

    No, you cannot do that, but there is nothing wrong with doing two queries.

           mysql_query("UPDATE pages SET views=views+1 WHERE ID=$id");
    $sql = mysql_query("SELECT * FROM articles WHERE id=$id");
    

    Also, if id is the primary key you don't have to do LIMIT 1 here, id is unique, therefore it will always have only one result matching your condition.

提交回复
热议问题