473,408 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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 12988
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
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
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
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
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
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
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
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
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
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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
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...

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.