By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,515 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,515 IT Pros & Developers. It's quick & easy.

Difference between mysql_affected_rows() and mysql_num_rows()

P: n/a
Hi,
So I read the manual where it says to use mysql_affected_rows() for
everything except SELECT and SHOW, and use mysql_num_rows() for those
two, which actually return a result.

However, I wrote this little script below where I find that
mysql_num_rows() returns exactly the same output in the case of
SELECT, as mysql_affected_rows(). In the case of the INSERT,
mysql_num_rows() does not return anything.

So my question is:
Is it safe to use mysql_affected_rows() for everything? Is there any
compelling reason to use myysql_num_rows()?

A lot of times I just want to know if a particular record exists in a
table, and I'd like to just run a SELECT and see if I got any rows
back.

=====================================snip========= ==============
<?php
// How does mysql_affected_rows() compare with mysql_num_rows()?
// We take care of connection strings and such here.
require_once './include/dbappinclude.php5';

$qstring = "SELECT * FROM MyTable WHERE uid = \"0\"";
// MyQuery runs mysql_query and die's if there was an error.
$res = MyQuery($qstring);
$num = mysql_num_rows($res);
$aff = mysql_affected_rows();

print "SELECT: Affected rows: $aff. Num_rows: $num <br>";

$qstring = "INSERT INTO MyTable VALUES ('0','1234')";
$res = MyQuery($qstring);
$num = mysql_num_rows($res);
$aff = mysql_affected_rows();

print "INSERT: Affected rows: $aff. Num_rows: $num <br>";

?>

====================function MyQuery=============
// Run the query which is encoded in $q
function MyQuery($q) {
global $conn;
$result = mysql_query($q, $conn);
if (!$result) {
die("Invalid query -- $q -- " . mysql_error());
}
return $result;
}

Dec 15 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Dec 15, 12:43 am, Sandman <enjoylife_95...@hotmail.comwrote:
Hi,
So I read the manual where it says to use mysql_affected_rows() for
everything except SELECT and SHOW, and use mysql_num_rows() for those
two, which actually return a result.

However, I wrote this little script below where I find that
mysql_num_rows() returns exactly the same output in the case of
SELECT, as mysql_affected_rows(). In the case of the INSERT,
mysql_num_rows() does not return anything.

So my question is:
Is it safe to use mysql_affected_rows() for everything? Is there any
compelling reason to use myysql_num_rows()?

A lot of times I just want to know if a particular record exists in a
table, and I'd like to just run a SELECT and see if I got any rows
back.

=====================================snip========= ==============
<?php
// How does mysql_affected_rows() compare with mysql_num_rows()?
// We take care of connection strings and such here.
require_once './include/dbappinclude.php5';

$qstring = "SELECT * FROM MyTable WHERE uid = \"0\"";
// MyQuery runs mysql_query and die's if there was an error.
$res = MyQuery($qstring);
$num = mysql_num_rows($res);
$aff = mysql_affected_rows();

print "SELECT: Affected rows: $aff. Num_rows: $num <br>";

$qstring = "INSERT INTO MyTable VALUES ('0','1234')";
$res = MyQuery($qstring);
$num = mysql_num_rows($res);
$aff = mysql_affected_rows();

print "INSERT: Affected rows: $aff. Num_rows: $num <br>";

?>

====================function MyQuery=============
// Run the query which is encoded in $q
function MyQuery($q) {
global $conn;
$result = mysql_query($q, $conn);
if (!$result) {
die("Invalid query -- $q -- " . mysql_error());
}
return $result;

}
num_rows returns the number of rows that were found by a SELECT
statement. affected_rows returns the number of rows that were altered
by a statement that modified the content of a database (insert,
update, delete etc)
Dec 15 '07 #2

P: n/a
Sandman wrote:
Hi,
So I read the manual where it says to use mysql_affected_rows() for
everything except SELECT and SHOW, and use mysql_num_rows() for those
two, which actually return a result.

However, I wrote this little script below where I find that
mysql_num_rows() returns exactly the same output in the case of
SELECT, as mysql_affected_rows(). In the case of the INSERT,
mysql_num_rows() does not return anything.

So my question is:
Is it safe to use mysql_affected_rows() for everything? Is there any
compelling reason to use myysql_num_rows()?

A lot of times I just want to know if a particular record exists in a
table, and I'd like to just run a SELECT and see if I got any rows
back.

=====================================snip========= ==============
<?php
// How does mysql_affected_rows() compare with mysql_num_rows()?
// We take care of connection strings and such here.
require_once './include/dbappinclude.php5';

$qstring = "SELECT * FROM MyTable WHERE uid = \"0\"";
// MyQuery runs mysql_query and die's if there was an error.
$res = MyQuery($qstring);
$num = mysql_num_rows($res);
$aff = mysql_affected_rows();

print "SELECT: Affected rows: $aff. Num_rows: $num <br>";

$qstring = "INSERT INTO MyTable VALUES ('0','1234')";
$res = MyQuery($qstring);
$num = mysql_num_rows($res);
$aff = mysql_affected_rows();

print "INSERT: Affected rows: $aff. Num_rows: $num <br>";

?>

====================function MyQuery=============
// Run the query which is encoded in $q
function MyQuery($q) {
global $conn;
$result = mysql_query($q, $conn);
if (!$result) {
die("Invalid query -- $q -- " . mysql_error());
}
return $result;
}

mysql_affected_rows() for a SELECT indicates the number of rows which
were found. mysql_num_rows() indicates how many rows were actually
returned. They may not be the same, IIRC, if you have a LIMIT clause or
similar. GROUP BY may also cause a difference.

If you need the number of rows returned, always use mysql_num_rows().
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Dec 15 '07 #3

P: n/a
On Dec 15, 1:43 am, Sandman <enjoylife_95...@hotmail.comwrote:
Hi,
So I read the manual where it says to use mysql_affected_rows() for
everything except SELECT and SHOW, and use mysql_num_rows() for those
two, which actually return a result.

However, I wrote this little script below where I find that
mysql_num_rows() returns exactly the same output in the case of
SELECT, as mysql_affected_rows(). In the case of the INSERT,
mysql_num_rows() does not return anything.

So my question is:
Is it safe to use mysql_affected_rows() for everything? Is there any
compelling reason to use myysql_num_rows()?

A lot of times I just want to know if a particular record exists in a
table, and I'd like to just run a SELECT and see if I got any rows
back.

=====================================snip========= ==============
<?php
// How does mysql_affected_rows() compare with mysql_num_rows()?
// We take care of connection strings and such here.
require_once './include/dbappinclude.php5';

$qstring = "SELECT * FROM MyTable WHERE uid = \"0\"";
// MyQuery runs mysql_query and die's if there was an error.
$res = MyQuery($qstring);
$num = mysql_num_rows($res);
$aff = mysql_affected_rows();

print "SELECT: Affected rows: $aff. Num_rows: $num <br>";

$qstring = "INSERT INTO MyTable VALUES ('0','1234')";
$res = MyQuery($qstring);
$num = mysql_num_rows($res);
$aff = mysql_affected_rows();

print "INSERT: Affected rows: $aff. Num_rows: $num <br>";

?>

====================function MyQuery=============
// Run the query which is encoded in $q
function MyQuery($q) {
global $conn;
$result = mysql_query($q, $conn);
if (!$result) {
die("Invalid query -- $q -- " . mysql_error());
}
return $result;

}
hi

indeed, for select statements affected_rows is equal to num_rows, but
only in the recent versions of libmysql. For backwards compatibility,
I'd still recommend num_rows for selects.

--
gosha bine

[gui for google chart api http://www.tagarga.com/files/gcui]
[php made right http://code.google.com/p/pihipi]
Dec 15 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.