mysql_query return value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bruce A. Julseth

    #1

    mysql_query return value

    I know the following $sql will fail since there is no Customer='Smith '. I
    want to determine how to test a failure of mysql_query.

    I thought mysql_query returned false if a query failed. The test fail path
    is never taken in the following code is never taken.

    Also, I get an invalid argument on mysql_num_rows( $Result) error message.

    $sql = "Update Table Set Password='passw ord' where Customer='Smith ';

    $Connection = mysql_connect(" localhost", "User", "PW") Or
    die(mysql_error ());

    $DB = mysql_select_db ("Database", $Connection);

    $Result = mysql_query($sq l, $Connection) or die(mysql_error ());

    if(!$Result) {

    echo "Fail<br>";

    }

    $Rows = mysql_num_rows( $Result) or die(mysql_error ());

    if($Rows > 0) {

    echo "Success Rows: $Rows<br>";

    } else {

    echo "Failure<br >";

    }

    What do I need to do to test for a mysql_query failure?

    Thank you...

    Bruce



  • Tom Thackrey

    #2
    Re: mysql_query return value


    On 16-Nov-2003, "Bruce A. Julseth" <bruceaj@attglo bal.net> wrote:
    [color=blue]
    > I know the following $sql will fail since there is no Customer='Smith '. I
    > want to determine how to test a failure of mysql_query.
    >
    > I thought mysql_query returned false if a query failed. The test fail path
    > is never taken in the following code is never taken.
    >
    > Also, I get an invalid argument on mysql_num_rows( $Result) error message.
    >
    > $sql = "Update Table Set Password='passw ord' where Customer='Smith ';
    >
    > $Connection = mysql_connect(" localhost", "User", "PW") Or
    > die(mysql_error ());
    >
    > $DB = mysql_select_db ("Database", $Connection);
    >
    > $Result = mysql_query($sq l, $Connection) or die(mysql_error ());
    >
    > if(!$Result) {
    >
    > echo "Fail<br>";
    >
    > }
    >
    > $Rows = mysql_num_rows( $Result) or die(mysql_error ());
    >
    > if($Rows > 0) {
    >
    > echo "Success Rows: $Rows<br>";
    >
    > } else {
    >
    > echo "Failure<br >";
    >
    > }
    >
    > What do I need to do to test for a mysql_query failure?[/color]

    The query will not 'fail' it will not affect any rows. Use
    mysql_affected_ rows() and check for zero.

    --
    Tom Thackrey

    tom (at) creative (dash) light (dot) com
    do NOT send email to jamesbutler@wil lglen.net (it's reserved for spammers)

    Comment

    • Bruce A. Julseth

      #3
      Re: mysql_query return value

      Thanks... Works like a champ....

      Bruce

      "Tom Thackrey" <use.signature@ nospam.com> wrote in message
      news:ZMYtb.1065 0$0E6.9470@news svr29.news.prod igy.com...[color=blue]
      >
      > On 16-Nov-2003, "Bruce A. Julseth" <bruceaj@attglo bal.net> wrote:
      >[color=green]
      > > I know the following $sql will fail since there is no Customer='Smith '.[/color][/color]
      I[color=blue][color=green]
      > > want to determine how to test a failure of mysql_query.
      > >
      > > I thought mysql_query returned false if a query failed. The test fail[/color][/color]
      path[color=blue][color=green]
      > > is never taken in the following code is never taken.
      > >
      > > Also, I get an invalid argument on mysql_num_rows( $Result) error[/color][/color]
      message.[color=blue][color=green]
      > >
      > > $sql = "Update Table Set Password='passw ord' where[/color][/color]
      Customer='Smith ';[color=blue][color=green]
      > >
      > > $Connection = mysql_connect(" localhost", "User", "PW") Or
      > > die(mysql_error ());
      > >
      > > $DB = mysql_select_db ("Database", $Connection);
      > >
      > > $Result = mysql_query($sq l, $Connection) or die(mysql_error ());
      > >
      > > if(!$Result) {
      > >
      > > echo "Fail<br>";
      > >
      > > }
      > >
      > > $Rows = mysql_num_rows( $Result) or die(mysql_error ());
      > >
      > > if($Rows > 0) {
      > >
      > > echo "Success Rows: $Rows<br>";
      > >
      > > } else {
      > >
      > > echo "Failure<br >";
      > >
      > > }
      > >
      > > What do I need to do to test for a mysql_query failure?[/color]
      >
      > The query will not 'fail' it will not affect any rows. Use
      > mysql_affected_ rows() and check for zero.
      >
      > --
      > Tom Thackrey
      > www.creative-light.com
      > tom (at) creative (dash) light (dot) com
      > do NOT send email to jamesbutler@wil lglen.net (it's reserved for spammers)[/color]


      Comment

      • OSIRIS

        #4
        Re: mysql_query return value

        Bruce A. Julseth wrote:
        [color=blue]
        > I know the following $sql will fail since there is no Customer='Smith '. I
        > want to determine how to test a failure of mysql_query.
        >
        > I thought mysql_query returned false if a query failed. The test fail path
        > is never taken in the following code is never taken.
        >
        > Also, I get an invalid argument on mysql_num_rows( $Result) error message.
        >
        > $sql = "Update Table Set Password='passw ord' where Customer='Smith ';
        >
        > $Connection = mysql_connect(" localhost", "User", "PW") Or
        > die(mysql_error ());
        >
        > $DB = mysql_select_db ("Database", $Connection);
        >
        > $Result = mysql_query($sq l, $Connection) or die(mysql_error ());
        >
        > if(!$Result) {
        >
        > echo "Fail<br>";
        >
        > }
        >
        > $Rows = mysql_num_rows( $Result) or die(mysql_error ());
        >
        > if($Rows > 0) {
        >
        > echo "Success Rows: $Rows<br>";
        >
        > } else {
        >
        > echo "Failure<br >";
        >
        > }
        >
        > What do I need to do to test for a mysql_query failure?
        >
        > Thank you...
        >
        > Bruce
        >
        >
        >[/color]
        I think it's an error in your script:

        you test this:
        if(!$Result) {

        echo "Fail<br>";

        }

        But if the $sql fails you continue the script with
        $Rows = mysql_num_rows( $Result) or die(mysql_error ());
        or the sql has failed.
        You should use a else:
        [color=blue]
        > $sql = "Update Table Set Password='passw ord' where Customer='Smith ';
        >
        > $Connection = mysql_connect(" localhost", "User", "PW") Or
        > die(mysql_error ());
        >
        > $DB = mysql_select_db ("Database", $Connection);
        >
        > $Result = mysql_query($sq l, $Connection) or die(mysql_error ());
        >
        > if(!$Result) {
        >
        > echo "Fail<br>";
        >
        > }
        >
        > ELSE {
        > $Rows = mysql_num_rows( $Result) or die(mysql_error ());
        >
        > if($Rows > 0) {
        >
        > echo "Success Rows: $Rows<br>";
        >
        > } else {
        >
        > echo "Failure<br >";
        >
        > }
        > }[/color]

        Try it maybe it's your answer.

        Comment

        • Gordon Burditt

          #5
          Re: mysql_query return value

          >I know the following $sql will fail since there is no Customer='Smith '. I[color=blue]
          >want to determine how to test a failure of mysql_query.[/color]

          There are failures and then there are failures.

          mysql_query will fail when it couldn't even DO the query for
          one or more of various reasons:

          - Syntax error in the query (e.g. "elect * from ... ")
          - Reference to nonexistent tables or columns
          - Permissions problems (e.g. UPDATE when you have read-only access to a table)
          - Network trouble talking to the server (but to get this far, you did
          successfully connect).
          - All sorts of problems with corrupted tables and/or bad disk sectors.
          and lots of other stuff I didn't think of.

          [color=blue]
          >I thought mysql_query returned false if a query failed. The test fail path
          >is never taken in the following code is never taken.[/color]

          If you consider UPDATE not changing anything to be a failure, you
          can check mysql_affected_ rows() after an UPDATE, DELETE, or INSERT.
          Beware, however, that mysql_affected_ rows() will return 0 if
          Smith's password was already 'password'. Do you consider THAT to
          be a "failure"? This tends to be a religious issue.
          [color=blue]
          >Also, I get an invalid argument on mysql_num_rows( $Result) error message.[/color]

          mysql_num_rows( ) only works after a query that returns a result set
          (even a 0-row result set). PHP documents this as only working after
          a SELECT, but MySQL says it works after any query that returns a
          result set (SELECT, SHOW, DESCRIBE, EXPLAIN, and I'm not sure that's
          all of them). In any case, it doesn't work after a query that does
          not return a result set (INSERT, UPDATE, DELETE, ALTER TABLE, etc.)
          [color=blue]
          > $sql = "Update Table Set Password='passw ord' where Customer='Smith ';
          > $Connection = mysql_connect(" localhost", "User", "PW") Or
          >die(mysql_erro r());
          > $DB = mysql_select_db ("Database", $Connection);
          > $Result = mysql_query($sq l, $Connection) or die(mysql_error ());
          > if(!$Result) {
          > echo "Fail<br>";
          > }
          > $Rows = mysql_num_rows( $Result) or die(mysql_error ());
          > if($Rows > 0) {
          > echo "Success Rows: $Rows<br>";
          > } else {
          > echo "Failure<br >";
          > }
          > What do I need to do to test for a mysql_query failure?[/color]


          Gordon L. Burditt

          Comment

          Working...