Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP and MySQL All or noting

Options
  • 01-09-2011 5:25pm
    #1
    Registered Users Posts: 22


    I'm updating multiple tables and don't know how make sure all of my queries go through. If one out of ten queries failed there would be database integrity problems.
    How do i make sure none of the queries go through if one failed?


Comments

  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    Ancel wrote: »
    I'm updating multiple tables and don't know how make sure all of my queries go through. If one out of ten queries failed there would be database integrity problems.
    How do i make sure none of the queries go through if one failed?

    Read about MySQL transactions, they do exactly what you are looking for.


  • Registered Users Posts: 22 Ancel


    ChRoMe wrote: »
    Read about MySQL transactions, they do exactly what you are looking for.
    Perfect Thanks


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    The mysql_query() function returns a value.

    Regardless of the type of query, it returns FALSE if the query failed.

    So for every query, execute

    $result = mysql_query($query, $link);

    Then immediately after, check

    if($result) {
    // Start on the next query
    } else {
    // Handle the error
    }

    There is also a function mysql_error() which will retrieve the error text from the last query. If the query was successful, mysql_error() gives an empty string. So that's another way of checking if the last query worked.


    Edit:

    Transactions are more appropriate in stored procedures and triggers. You typically wouldn't use transactions within PHP code because mysql_query() can only execute one statement at a time and not an entire block.


  • Registered Users Posts: 22 Ancel


    @seamus

    How would i rollback the changes if I'm 4/10 queries down and the 5th fails?

    EDIT:
    http://www.daniweb.com/web-development/php/threads/152659
    I think R0bb0b has the idea.


  • Registered Users Posts: 3,140 ✭✭✭ocallagh


    @seamus, there are quite a few ways to use transactions within PHP.

    mysqli which is generally available is a simple way to use it but also available with mysql. You still use a bunch of connections to the server though (which is why stored procedures or triggers are better), but they will still work fine.


  • Advertisement
  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    It's probably just personal preference on my part.

    I prefer to do error/flow control exclusively in PHP or MySQL, or both (if you need high integrity), but not a mix of SQL code in PHP.

    In the above scenario where 10 tables need to be updated or rollback on failure, I'd be inclined to implement a stored procedure and let MySQL manage any issues.

    But as I say, just personal preference probably.

    [Edit: And yes, I missed the last line of the OP, so transactions are the way to go with this, but I'd use SPs]


  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    seamus wrote: »
    It's probably just personal preference on my part.

    I prefer to do error/flow control exclusively in PHP or MySQL, or both (if you need high integrity), but not a mix of SQL code in PHP.

    In the above scenario where 10 tables need to be updated or rollback on failure, I'd be inclined to implement a stored procedure and let MySQL manage any issues.

    But as I say, just personal preference probably.

    Agreed, where possible the most amount of work should be done on the database.


Advertisement