enchance
enchance

Reputation: 30501

Simulate a failed mysql INSERT query

Is there a way to simulate a failed INSERT query in mysql? Since I'm running PHP/MySQL locally, the return value will always be TRUE. Turning of MySQL in xampp doesn't seem to work.

NOTE: I may be looking at this the wrong way. Feel free to let me know.

Upvotes: 1

Views: 960

Answers (1)

Marcin Orlowski
Marcin Orlowski

Reputation: 75636

Couple of approaches:

  • revoke INSERT permission for your database user
  • wrap your DB access code into middle layer and simulate failures there

Or if you just need to test INSERTS, create dumb function like:

function my_query( $link, string $query ) {
  if( rand() %1 ) {
    return false;
  } else {
    return mysqli_query( $link, $query );
  }
}

and update your code that INSERTs to call my_query instead. Tune your failure conditions and happy testing :)

Upvotes: 1

Related Questions