Hi,

This is maybe a bit more of a MySQL question, although the MySQL insert query is working correctly.

I'm inserting some data ($category) into a MySQL table (test), and trying to get some feedback in the event that either the query is successful or unsuccessful.

I’m using the below code, but for some reason mysql_affected_rows continuously returns false. I have checked the table, and the information is being inserted, so I know the insert query is successful. Why isn’t mysql_affected_rows detecting the update?

$result=mysql_query("INSERT INTO `test` (`data`) VALUES ('$category')");
	if (mysql_affected_rows($result) < 1){
    $feedback .=  ' ERROR - New Category not Added - Database Error ';
    return false;
    } else {
            $feedback .=  ' SUCCESS - New Category Added ';
            return true;
    }

    Do a [man]var_dump/man on mysql_affected_rows() after you perform the query - what value is it returning?

      Hi,

      Thanks for the reply.

      var_dump(mysql_affected_rows($result)) returns:

      bool(false)

        Hang on,

        var_dump(mysql_affected_rows()) returns:

        int(1)

        Shouldn't the output be the same for both? mysql_affected_rows returns the number of rows affected by the last query -'If the link identifier is not specified, the last link opened by mysql_connect() is assumed' - in this case the last query being $result?

          Woops, been too long since I've used the MySQL library...

          Your problem is that [man]mysql_affected_rows/man expects you to be passing it either nothing (in which it assumes the last connection, as you pointed out) or a MySQL link identifier. You're passing it something else, which is why it's returning FALSE due to an error.

            passing it a resource or false if the query failed.

              igorek;10959314 wrote:

              passing it a resource

              Yes, but the wrong resource. 🙂

                Thanks for the replies.

                What threw me was I'd used 'mysql_numrows' in a previous query, in exactly the same way (code below), and that worked without any problems. And I expected mysql_affected_rows to work in the same way!

                $sql="SELECT * FROM `users` WHERE `username`='$username' AND `password`=sha1('$password')";
                	$result=mysql_query($sql);
                	if (!$result || mysql_numrows($result) < 1){
                	$feedback .=  ' ERROR - User not found or password incorrect ';
                	return false;
                	}
                  Write a Reply...