Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    13
    Rep Power
    0

    An invalid parameter was passed to sqlsrv_rows_affected


    Hi,
    I am trying to get the sqlsrv_rows_affected count using php and for some reason I get this error

    Warning: sqlsrv_rows_affected() expects parameter 1 to be resource, boolean given in

    I have checked my code again and again but to no avail.

    My code is

    $this->connect();
    $tsql ="insert into newsletter(fname,email)values(?,?)";
    $params = array($fname,$email);
    $stmt=sqlsrv_query( $this->connectionString, $tsql, $params);

    $rows_affected = sqlsrv_rows_affected( $stmt);
    if( $rows_affected === false)
    {
    echo "Error in calling sqlsrv_rows_affected.\n";
    die( print_r( sqlsrv_errors(), true));
    }
    elseif( $rows_affected == -1)
    {
    echo "No information available.\n";
    }
    else
    {
    echo $rows_affected." rows were updated.\n";
    }

    Just a note: Although I get this error when I check the database the data seems to be inserted.

    Would really appreciate any of your help.

    Thanks
    Ryan
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,497
    Rep Power
    594
    1) Please enclose your code in [ PHP ]. See the sticky at the top of this forum.
    2) The error means $stmt is not set. Apparently the query failed ( as opposed to returning 0 rows) and that is where you need to focus your attention.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    13
    Rep Power
    0
    Hi gw1500se,

    Thanks for your reply.

    The $stmt should be set since the data in being inserted properly. Its only when I try to fetch the affected rows it throws the error.

    I did this to check if $stmt is set and I get no errors. I get
    "data inserted".

    [

    if($stmt===false){

    echo"<br/> data insertion error";


    }else{

    echo"<br/> data inserted";

    }

    ]

    Thanks
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,497
    Rep Power
    594
    Wrong check. Use this.
    PHP Code:
    if (is_resource($stmt)) {
       echo
    "<br/> data inserted";
    }
    else {
       echo
    "<br/> data insertion error";

    Note the use of [ PHP ] tags. See the sticky at the top of this forum that says "READ THIS BEFORE POSTING".
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    No, the check is correct, since the return value is either false or a resource.

    So what happens when you put the affected rows in the "not false" part?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    13
    Rep Power
    0
    Hi gw1500se,

    I implemented the code as you suggested and it throws " data insertion error".

    When I go and look back into the database the data seems to get inserted. Was just thinking if $stmt is failing shouldn't the data insertion fail as well ?.

    Here's my code

    Code:
    $tsql ="insert into newsletter(fname,email)values(?,?)";
    
    $params = array($fname,$email);
    
    $stmt=sqlsrv_query( $this->connectionString, $tsql, $params);
    
    if (is_resource($stmt)) {
       echo"<br/> data inserted";
    }
    else {
       echo"<br/> data insertion error";
    }
    I am working on my university coursework. You help is really appreciated.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,497
    Rep Power
    594
    I thought using false was unreliable with returned objects. Thanks for the correction.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    13
    Rep Power
    0
    Hi Jacques1,

    Thanks for your reply. When I output the value of affected rows it just come blank and outputs the error msg

    arning: sqlsrv_rows_affected() expects parameter 1 to be resource, boolean given in

    Thanks
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,497
    Rep Power
    594
    Go back an look at it from the data insertion error perspective. Are you sure it is really inserting the data? Perhaps what you are looking at is a record that was there previously and that is why you get that error. Be sure you delete that record before rerunning your script.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    13
    Rep Power
    0
    Hi gw1500se,

    I deleted all data/records from that table and re ran the script, it seems to insert the data.

    Any view?.

    Thanks
  20. #11
  21. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Please show your updated code including(!) the affected rows stuff, you've obviously made a mistake while putting in the "if" statement.

    If the return value is not false, it cannot be a boolean. So it cannot pass the "not false" check and still be a boolean.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,497
    Rep Power
    594
    It is hard to believe that the record was inserted correctly while getting that error. Did you try running that EXACT query from command line and do you get the same result?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  24. #13
  25. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    If the table had a unique key on one of the columns, and he was attempting to insert duplicate data for that column, that could explain the error, no?
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    13
    Rep Power
    0
    Hi Jacques1,

    This is my code

    Code:
    $tsql ="insert into newsletter(fname,email)values(?,?)";
    
    $params = array($fname,$email);
    
    $stmt=sqlsrv_query( $this->connectionString, $tsql, $params);
    
    $rows_affected=sqlsrv_rows_affected( $stmt);
    
    if($rows_affected>0 && $stmt){
      echo"<br/> data inserted";
    
    }else{
        echo sqlsrv_errors();
    }
    Even if I jus change this line if($rows_affected>0 && $stmt) to
    if($rows_affected>0 ) it gives me the same error
    An invalid parameter was passed to sqlsrv_rows_affected

    Thanks
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,497
    Rep Power
    594
    Changing the if statement AFTER the error will do nothing. Try inserting this before setting $rows_affected.
    PHP Code:
    echo "<pre>";
    var_dump($stmt);
    echo 
    "</pre>"
    Note the use of [ PHP ] tags.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo