Re: note 8981 added to mysql_affected_rows
| From: | Lars Torben Wilson | Date: | Tue, 10 Oct 2000 06:36:24 +0000 |
| Subject: | Re: note 8981 added to mysql_affected_rows | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-6294@lists.php.net to get a copy of this message | ||
foo@ender.com writes:
> This definitely changed, somehow, from php3->php4. I used to do things like:
>
> $result = mysql_result($query);
> if (@mysql_affected_rows($result) > 0) {do stuff}
>
> It worked fine. In 4, it started complaining that
> $result was not a link identifier index -- I took
> out $result and just used mysql_affected_rows(),
> and got the expected result.
>
> http://www.php.net/manual/function.mysql-affected-rows.php
This note has been deleted; if you have a bug report, you should put
it in the bug database at http://bugs.php.net. Please make sure to
read the instructions on the page where you submitted this note before
adding another one.
At any rate, the code above is broken. It would have worked in PHP 3,
but probably not for the reasons you thought it did.
Since mysql_result() like that would have failed every
time--mysql_result() takes at least 2 arguments--and the code above
worked in PHP 3, I'm guessing that you meant mysql_query()--using
mysql_result() there would have meant that the if() test could never
succeed.
Note that the thing you pass to mysql_affected_rows() is a link id
created with mysql_connect(), not anything to do with any specific
query. If you pass nothing to mysql_affected_rows(), then it will use
the most recently created link id.
In PHP 3, a link id was represented by a simple integer. however,
in PHP 4, it is a new animal altogether. To illustrate, try putting an
the following line:
echo "Link ID: $link_id; of type " . gettype($link_id) . "<br>\n";
after the mysql_connect() line in the code snippet above. In PHP 3,
you get:
Link ID: 1; of type integer
In PHP 4, you get:
Link ID: Resource id #1; of type resource
Try out the following code:
<?php
error_reporting(E_ALL);
$host = 'localhost';
$user = 'myuser';
$pass = 'mypass';
$db = 'mydb';
$link_id = mysql_connect($host, $user, $pass);
echo "Link ID: $link_id; of type " . gettype($link_id) . "<br>\n";
mysql_select_db($db);
$result = mysql_query('insert into sometable set somecolumn = "testing"');
$affected_rows = mysql_affected_rows($result);
echo "Affected $affected_rows row.<br>\n";
?>
In PHP 3, this would work because if the mysql_query() succeeded, it
would return a 1--which would be likely to be the same as the link
identifier from the mysql_connect() call. This 1, unrelated to the 1
value of the link id, would then get passed to mysql_affected_rows().
Thus, although mysql_affected_rows() would be acting correctly, it
would be doing so for the wrong reason.
In PHP 4, however, the link id is not a simple integer; hence, it
breaks. What you need to give mysql_affected_rows() is either the link
id from the mysql_connect() call, or leave it empty, in which case it
will use the most recently opened db link.
Hope this clears it up a bit,
Torben
--
+----------------------------------------------------------------+
|Torben Wilson <torben@php.net> Netmill iTech|
|http://www.coastnet.com/~torben http://www.netmill.fi|
|Ph: 1 250 383-9735 torben@netmill.fi|
+----------------------------------------------------------------+