SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Almeaty Member
    Join Date
    Jul 2000
    Location
    Nowhere
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    what is the solution for checking whether mysql_query was successful or not? For example, when I $result=mysql_query( "CREATE TABLE....." ); and afterwards do if ( $result ) { // was successful }:

    Will this code in the if-condition repeat the query or just check if the variable is not empty? What about this: if ( !$result ) { // not successful }

    thx

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $result will be true if the query was successful and false if the result wasn't.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    So if ($result) { ... } executes the code in braces if the query was successful, and if (!$result) { ... } executes the code in braces if the query failed. Neither of these re-do the query -- the query is performed by mysql_query(), and the result is stored in $result, along with an indication of success or failure (which the above code fragments use).
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  4. #4
    SitePoint Evangelist
    Join Date
    May 2000
    Location
    Canada
    Posts
    533
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i prefer using mysql_num_rows() and mysql_affected_rows() so if its 0, i know something is wrong ... because, the query will FAIL only when the query is incorrect

    --------------------
    SitePoint Community Advisor
    myPHPhost: when you want something MORE from a host

  5. #5
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yep, Vinay. It really depends on the circumstace. Some of my queries are expected not to affect any rows sometimes, but I still check them for error. Others I'll often check for error and then check for rows affected.

    I always check for error, though, because if done using the 'or' operator there is extremely little overhead and it's a nice fail-safe.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •