Select and Update in same query

前端 未结 3 1342
野的像风
野的像风 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:12

    Basically, NO, you cannot SELECT and UPDATE with the same query. Your only option would be to write a STORED PROCEDURE which is a more advanced technique of database interaction.

    0 讨论(0)
  • 2021-01-13 06:28

    As others have observed, you can send multiple commands to the database with separate invocations of the relevant function call. It is rarely (if ever) necessary to submit multiple commands to the server within a single function call.

    However, it can be done (and it is to answer that question in the generality that I write this post—in your particular case, it should be avoided). Note that enabling multi-statements increases the risk of SQL injection attack and should therefore rarely ever be done (and, at that, only when sufficient alternative precautions have been taken to avoid or to mitigate against such attacks).

    The ancient ext/mysql extension (which you are using, but which has been deprecated in PHP v5.5 and whose use in new code has been strongly discouraged for years) has no official support for multi-statements. Indeed, the documentation clearly states:

    Description

    resource mysql_query ( string $query [, resource $link_identifier = NULL ] )

    mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.

    However, this restriction is only enforced at the driver level and can be overridden (in an undocumented/unsupported) manner by setting the driver's CLIENT_MULTI_STATEMENTS connection flag as shown below (or else by recompiling PHP to enable such by default):

    mysql_connect($host, $user, $pass, false, 65536);
    mysql_select_db($db);
    mysql_query("
      SELECT * FROM articles WHERE id='$id' LIMIT 1;
      UPDATE pages SET views=views+1 WHERE ID=$id;
    ");
    

    A serious down-side to this approach is that one can only access the result of the first command. Should subsequent commands return results (or even fail), such information will be unavailable to your application.

    MySQLi, which is the modern replacement for ext/mysql, natively supports multi-statements without such hackery. PDO, a more generic database abstraction library, can also support multi-statements in some circumstances.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题