Pdo how many rows returned




















Keep in mind that you cant fetch results twice. For example:. If you just want to get a count of rows not the data ie. Actually retrieving all the rows data to perform a simple count is a waste of resources. If the result set is large your server may choke on it. To use variables within a query you have to use bindValue or bindParam. And do not concatenate the variables with ". I decided to used a for loop as show below. For straight queries where I want a specific row, and want to know if it was found, I use something like:.

Now, if you want to know how many rows are existing in your table and you have for example column 'id' as the primary key, the query to DB will be:. Or, if you want to know how many products there are in the table 'products' have the price between 10 and 20, write this query:.

Edit: sorry for the wrong post but as some example show query with count in, I was suggesting using the mysql result, but if you don't use the count in sql fetchAll is efficient, if you save the result in a variable you won't loose a line. You may need to add more 'handlers', though, depending on what commands you use. You can combine the best method into one line or function, and have the new query auto-generated for you:.

How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 12 years, 6 months ago. Active 7 months ago.

Viewed k times. Do you have any suggestions? James James 5, 14 14 gold badges 45 45 silver badges 72 72 bronze badges. Add a comment. Active Oldest Votes. Your Common Sense k 35 35 gold badges silver badges bronze badges.

I assume he has already done a select query and now wants to know how many rows were returned. Using this approach, fetchColumn returns a string "" I'd also recommend type casting the result from fetchColumn to an integer. The prepared statement works fine.

What can be the issue? Show 7 more comments. However, this function is deprecated and will be removed in the future. Chad Birch Chad Birch Thanks again. Option 2 is not advisable if the result is big. Show 2 more comments. People are coming here having two different tasks in mind: They need to know how many rows in the table They need to know whether a query returned any rows That's two absolutely different tasks that have nothing in common and cannot be solved by the same function.

Counting the number rows returned. Say, if you are selecting only one row. Counting the number of rows in a large resultset In such a rare case when you need to select a real huge amount of rows in a console application for example , you have to use an unbuffered query , in order to reduce the amount of memory used.

Your Common Sense Your Common Sense k 35 35 gold badges silver badges bronze badges. I bet you already learned that the first one simply doesn't work. So you have no choice but the second one :. However, ewhen possible, it's OK to chain the method calls, it is even called so, "the method chaining". It is usually however is called by rows nevertheless, for the better readability let's pretend it works :.

Select the number of rows using PDO Comments 1 When you need to count rows that match some criteria in your database, under no circumstances you should select the actual rows and then use rowCount! Got a question? Click here to ask! Latest comments: I searched for hours before I found your solution. Parameters This function has no parameters. Return Values Returns the number of rows.

Return number of rows that were deleted: Deleted 9 rows. When updating a Mysql table with identical values nothing's really affected so rowCount will return 0. As Mr. Perl below noted this is not always preferred behaviour and you can change it yourself since PHP 5.

I'm not so sure I'm diggin' PDO yet. For MySQL, it will return 1 if the row is inserted, and 2 if it is updated, but that may not apply to other databases. It has many advantages as you could retrieve only part of result set via LIMIT but still get the total row count. BUT if your query is unbuffered, than it will return 0.



0コメント

  • 1000 / 1000