Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$wpdb - what does it return on fail?

I'm not sure whether this question is WordPress specific or is more related to mySQL. I am trying to find out what would be returned if the transaction with the database fails. In the following scenario, I am updating a row. If none of the values are changed false is returned. If changes are made true is returned. How can I tell if the transaction has failed?

$result = $wpdb->update($this->table_name, $dbfields, $where);
if($result == false)//do fail - this is not really a fail!
if($result == true)//do success

Any pointers appreciated.

like image 418
Chin Avatar asked Jun 30 '11 02:06

Chin


People also ask

What is the $Wpdb variable in WordPress?

The $wpdb object can be used to read data from any table in the WordPress database, not just those created by WordPress itself.

What is WPDP?

Washington Prescription Drug Program (WPDP) | Washington State Health Care Authority. Menu. Free or low-cost health careOpen submenu.

What is WP DB?

WordPress database is a storage of your website data using MySQL open-source database management system. All WordPress databases have a set default MySQL structure, which allows your website to work well, but you can add more tables to customize.


1 Answers

Take a look in wp-includes\wp-db.php. The header comments of wpdb's update function says:

 * @return int|false The number of rows updated, or false on error.

So, I'd suspect that you want to find the difference between false (a boolean value indicating a failure) and 0 (an integer indicating that no rows were returned.)

If you compare using ==, false and 0 are equal. You therefore need to use the === operator to check whether you're dealing with the boolean false or the integer 0.

So, try these:

if ($result === false) // Fail -- the "===" operator compares type as well as value
if ($result === 0) // Success, but no rows were updated
if ($result > 0) // Success, and updates were done. $result is the number of affected rows.

See the PHP manual for more information on the === comparison operator.

like image 50
Matt Gibson Avatar answered Oct 20 '22 00:10

Matt Gibson