How to know if rows are deleted using mysqli dbdriver - php

I am using codeigniter 2.1.0 with mysqli dbdriver.
I have the below query and would like to know when it is successful
$query = $this->db->query("delete FROM abc WHERE id IN (" . $IdList . ")");
How can I know if the related rows are deleted?

$this->db->affected_rows() is what you are looking for. You should be able to compare that result with the expected number of deleted rows.
Also quoting the USER GUIDE
Displays the number of affected rows, when doing "write" type queries (insert, update, etc.).
Note: In MySQL "DELETE FROM TABLE" returns 0 affected rows. The
database class has a small hack that allows it to return the correct
number of affected rows. By default this hack is enabled but it can be
turned off in the database driver file.

From the CodeIgniter User Guide:
The query() function returns a database result object when "read" type queries are run, which you can use to show your results. When "write" type queries are run it simply returns TRUE or FALSE depending on success or failure.
Therefore your $query variable will be TRUE on success or FALSE on failure.

Related

Is there a real difference between mysqli::num_rows and mysqli::affected_rows when doing a SELECT

I have been looking for a definate answer on this, but opinions on the internet slightly vary.
I am used to differentiating between using mysqli::num_rows and mysqli:affected_rows for the intended purposes. However, I read in the PHP manual that affected_rows will behave like num_rows when a select query has been executed.
My actual question is this: Is there any downside to ALWAYS using affected_rows and forgetting about num_rows? If affected_rows behaves like num_rows, what use is num_rows? Using only affected_rows would simplify my DB library code in PHP.
There are two types of operations related table records (Not table structure):
1) Select: When you are only fetch the data from table.
2) Update: When you updating the table records (Insert/Update/Delete) all these operations will update/change your table records.
If you run the select query the num_rows will work because it returns a count of rows. But when you run Insert/Update/Delete query affected_rows will work because it returns how many rows affected by your Insert/Update/Delete query.
Also, Mysql server returns affected rows when you run Insert/Update/Delete query. PHP function affected_rows convert this response directly with connection resource/object. But when you run select query mysql server returns result-set. And by using this result php function num_rows returns that result set has how many rows. For affected_rows we are using connection resource/object and for num_rows we are using result resource/object.

How to tell if PDO found a match to my UPDATE query, and updated or not?

UPDATE users SET 'name'='john' WHERE users.ID = 54;
actually all this question is about pdo::rowCount, as I need to know that a match to the query was:
1. found, but need not to update (matching values)
2. found, and updated
3. not found at all, (not updated)
I am looking for a way to tell in PHP one of those three options
As documented under UPDATE Syntax:
UPDATE returns the number of rows that were actually changed. The mysql_info() C API function returns the number of rows that were matched and updated and the number of warnings that occurred during the UPDATE.
In particular, after an UPDATE query mysql_info() returns ER_UPDATE_INFO. The MySQL Command-Line Tool displays such information immediately after a command is issued. For example:
mysql> UPDATE users SET name='john' WHERE ID=54;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
MySQLi provides direct access to this function, through mysqli::$info. Even the Original MySQL API provided access thereto, through its own mysql_info() function.
Unfortunately, PDO provides no access to this function—a search of the codebase confirms that it never calls mysql_info().
Alternatively, clients can set the CLIENT_FOUND_ROWS connection flag—then UPDATE will return the number of rows that matched rather than the number that were changed. PDO does provide access to set this connection flag, via the PDO::MYSQL_ATTR_FOUND_ROWS driver-specific attribute.
However this connection flag is of little use to you, as instead of being unable to differentiate between "not found at all" and "found but no update", it will leave you unable to differentiate between "found but no update" and "found and updated".
Thus, as others have already suggested, your only option in this case would be to perform a separate SELECT query to differentiate between the possible conditions. In order to avoid race hazards, you should be careful to perform the SELECT within the same transaction and to use a locking read if reading before updating.

PHP update query successful but database not updating

I am trying to update a record in my database and SQL query return successful but my database is not changing.
representative_id = '$login_session'
Is the representative_id column in your table a VARCHAR, or an INT? You're passing a string to MySQL, there. If the column in your DB is stored as an INT, try removing the single quotes around $login_session.
When posting questions relating to database queries, it helps to also include your table schema.
Seems like you're using PDO (hopefully)
When you're using this Data Objects, when a query is executed it will return TRUE, but it doesn't care if it changed something or not, it will return true when there is not a critical MYSQL Error.
What you want to do is to know IF A ROW AS AFFECTED / CHANGED by that query, in that case instead of using;
$PDOObject->query($thequery) == true
you should use:
$result = $PDOObject->query($thequery);
if($result->rowCount() > 0) ...
More info about how to know if a row was affected by a query with PDO
http://php.net/manual/en/pdostatement.rowcount.php
sql query return successful but my database is not changing
Are you sure that there exists at least one record that matches your filter criteria WHERE full_name = '$full_name' AND representative_id = '$login_session'. If no record updating means no any record matching the WHERE condition and so no updates occurring.

Why don't PHP's MySQL libraries associate affected_rows with the query result object?

This is a pretty straightforward question I think, but I'll explain my thinking:
Unless I'm mistaken, the number of affected rows is always inherently linked to a query that has been performed. Similarly, the number of rows returned by a SELECT are inherently linked to that query, and the value is retrieved as such:
echo $mysql_result_object->num_rows;
Why is it that to retrieve the affected rows from something like an UPDATE or DELETE statement, one should access the value through the connection object?
echo $mysql_connection_object->affected_rows;
It doesn't seem very logical to me.
Is it simply because query functions (for example mysqli_query) return boolean true for INSERT/UPDATE/DELETE statements? In which case... shouldn't they return an empty result object?
You pretty much answered your own questions, so I'm just clarifying really.
num_rows operates on the result set. It is therefore only valid for operations that return a result set. i.e. SELECT or SHOW.
INSERT, UPDATE and DELETE do not return a result set. To answer your last sentence; they cannot return an empty result set, since they are already returning a boolean.
affected_rows will return the number of affected rows for the last query associated with a link_identifier.
So, you cannot use num_rows for INSERT, UPDATE, DELETE, but you can use affected_rows for for a SELECT, in which case it operates like num_rows.

What MYSQL commands by definition do *not* return a result?

I'm writing a class in PHP for handling MySQLI queries, and when the query manages not to return a result set (or object) I want to issue a helpful error message. But, of course, not all queries are designed to return a result set (e.g. ALTER), so I want to filter out those queries that don't.
By my reckoning, the queries that may or may not return a result set are:
SELECT
SHOW
CREATE VIEW
...and all others (e.g. ALTER) do NOT return a result. But have I overlooked something? What are the query commands that never return a result, and what are the query commands that may or may not return a result?
Many thanks!
From the PHP manual for mysqli::query
Return Values
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
I suggest to just check if the return value is equal to false (=== false) and do something with mysqli_errno or mysqli_error if it is. Actual parsing of the query string is overkill.
If you must know whether it is a result set or just a boolean, check for the return type with is_bool($result) or $result instanceof mysqli_result.
There are two types basically:
Ones that return a result set - it may vary from empty set to a massive amount of data
Ones that return a count of affected rows.
There are also three types of queries:
DML - queries to manipulate data - select, insert, update, delete.
DDL - queries to figure out data schema - create table/view etc.
Control queries - grant or revoke permissions
I suppose control queries usually return nothing and DDL queries like create/alter/set also return nothing.
SELECT returns result sets (which may be empty). INSERT/UPDATE/DELETE return the number of rows affected.
I'd recommend reading on SQL queries for more information.

Categories