473,487 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Difference between mysql_affected_rows() and mysql_num_rows()

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
3 12989
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2085
by: David Nikel | last post by:
I am attempting to create a login script. The following snippet of code: $query = "select * from auth where username='$username' and password=password('$password')"; $result =...
1
1951
by: Ben | last post by:
Hi all, I want to store unique data about my page visit. This is done using cookie value. In my database, the page name and cookie value(ip address) are both primary keys. This ensures unique...
3
2412
by: lkrubner | last post by:
Suppose I make a call to MySql and zero rows come back. How do I tell the difference between zero rows and failure?
1
1806
by: Hebron Mak | last post by:
Has anyone ran into problems where mysql_num_rows() return 0 when I know the result set contains a certain number of rows? My code looks something like this: MYSQL_RES *result; int numRows =...
4
1743
by: Fish44 | last post by:
Ive got the following code which works great on my localserver, but when i upload it to my service provider i get an error "Warning: mysql_num_rows(): supplied argument is not a valid MySQL...
5
5224
by: Tyno Gendo | last post by:
Two questions in a row.. I'm on a roll :) When i perform UPDATEs in my admin system, I'm checking for success by using mysql_affected_rows() and this works fine if data is changed and updated. ...
4
7821
by: Ryanlawrence1 | last post by:
Heya, I get these 2 errors: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/themepar/public_html/changepass.php on line 20 You have not entered all the...
5
1996
by: MS | last post by:
Hi, I'm doing a table insert and have a question about mysql_affected_rows(). The following code works. It correctly inserts the row and mysql_affected_rows() returns 1 as it should do. ...
4
3610
by: seigaku | last post by:
Hi everyone, I've recently begun learning PHP and it looks like I took on too much so soon. I'm attempting to fix a php script that detects whether or not a person is Eligible to join a...
0
7106
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5442
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4874
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
600
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.