eHow launches Android app: Get the best of eHow on the go.

How To

How to Retrieve the Number of Rows Affected in a PHP/MySQL Query

Member
By Mike
User-Submitted Article
(0 Ratings)

I have to admit I am a convert form Perl to PHP. For many years I thought Perl was better, but then I started using PHP more and more when I began developing more advanced websites. I won't go into anything advanced in this tutorial, but I did want to give a step by step account as to how to return the number of rows affected when a MySQL query is run in PHP. In Perl it would be $DBI::rows will give you the number of rows, in PHP it is a bit different, but the concept is the same.

Here is how you obtain the number of rows affected when you run a query against a MySQL database in PHP.

Difficulty: Easy
Instructions
  1. Step 1

    Launch your PHP editor. I use notepad. Any text based tool will do. Notepad is free which is why I use it, but it won't give you the ability some PHP editors will like debugging and so on.

  2. Step 2

    The first thing you need to do is open your database. Ehow does not allow computer code to be written within the steps, so the code will be in images associated with each step.

  3. Step 3

    Run your query as shown in the picture associated with this step. This particular query says to select all of the records from a table named "some_table".

  4. Step 4

    The command to retrieve the number of rows returned in this particular selection query is called "mysql_affected_rows". Take a look at the code in the picture associated with this step. The code says that if the number of rows returned from this query is greater than 100 then print out (echo) the statement that reads "This query as more than 100 rows in it" otherwise don't print out anything. You can also assign "mysql_affected_rows" to a value if you need to use it later in your code.

Post a Comment

Post a Comment
  • Have you done this? Click here to let us know.
I Did This

Related Ads

Tags
Computers
Alexia Petrakos,

Meet Alexia Petrakos eHow's Computers Expert.

Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

eHow Computers
eHow_eHow Technology and Electronics