Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-06-2003, 04:58 PM   #1 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
Trying to count an array

I'm trying to count an array with the values output from mysql_affected_rows. The rows are deleted properly, but it returns '1' no matter how many rows are selected and deleted. Ideas?

PHP Code:
foreach ($del_row as $row_id) {
             
$result mysql_query("DELETE FROM $db_table WHERE id = $row_id");
        }
        
$deleted_rows = array(mysql_affected_rows());
        
$num_rows count($deleted_rows);
        echo 
$num_rows " deleted.\n<br><br>\n"
bdl is offline   Reply With Quote
Old 03-06-2003, 05:08 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
mysql_affected_rows() returns the total rows affected by '1' query. therefor, since you are doing a 'foreach' loop, your last query affected 1 row.

in your case, you are running a query that will affect 1 row no matter what.

this code will get you the total deleted rows:
PHP Code:
<?
$i
;
foreach (
$del_row as $row_id) {
             
$result mysql_query("DELETE FROM $db_table WHERE id = $row_id");
     
$i++;
        }
      
        echo 
$i" deleted.\n<br><br>\n";
?>
now here's an example where mysql_affected_rows might work better.
PHP Code:
<?
// lets say $some_var appears in the field for 20 rows
$result=mysql_query("delete  from table where field='$some_var'");
$deleted_rows=mysql_affected_rows();

echo 
$deleted_rows " deleted.\n<br><br>\n";
?>
that's a cool function.. i didn't know about it till now =)
sde is offline   Reply With Quote
Old 03-06-2003, 06:46 PM   #3 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
Thanks for the reply; didn't consider just using an incremented variable like that, good call, works perfectly. Using that particular function doesn't seem to work the way I want it to in this case but, mysql_affected_rows is a very cool little trick, I've used it several times. =)
bdl is offline   Reply With Quote
Old 03-07-2003, 06:34 AM   #4 (permalink)
Admin
$_['Your_Mom'];
 
Admin's Avatar
 
Join Date: May 2002
Location: Santee
Posts: 627
Admin is on a distinguished road
you can add things like count(XXX) into your query as well when doing a select.

where XXX is some column you want to count.
__________________


Urban Clothing
Admin is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
working with Array Goong MS Technologies ( ASP, VB, C#, .NET ) 3 07-22-2004 12:14 PM
breaking down an array from a form metazai PHP 12 07-09-2004 06:18 AM
array of characters in a structure TheDreamless Standard C, C++ 3 04-18-2003 04:30 PM
array max num Apodysophilia Java 9 04-10-2003 06:36 AM
adding to an array in php sde PHP 1 06-12-2002 11:04 AM


All times are GMT -8. The time now is 03:57 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting