catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.83k stars 1.15k forks source link

Update statement always returns `queryString`? #628

Closed nealoke closed 7 years ago

nealoke commented 7 years ago

Hi, first of all thanks for this superb lib 🔥

I'm currently using it a lot and for some reason with one statement I always receive the queryString back when performing ->update('table', $params, ['id' => $id]). This only happens with one case and I can't seem to figure out why. Any help would be awesome!

Example output eg. print_r($medoo->update('table', $params, ['id' => $id]))

object(PDOStatement)#11 (1) {
  ["queryString"]=>
  string(307) "UPDATE "clients" SET "name" = :MeDoOmEdOo_10, "accountLimit" = :MeDoOmEdOo_11, "website" = :MeDoOmEdOo_12, "email" = :MeDoOmEdOo_13, "road" = :MeDoOmEdOo_14, "houseNumber" = :MeDoOmEdOo_15, "city" = :MeDoOmEdOo_16, "country" = :MeDoOmEdOo_17, "preferredLanguage" = :MeDoOmEdOo_18 WHERE "id" = :MeDoOmEdOo_19"
}

->error()

array(3) {
  [0]=>
  string(5) "00000"
  [1]=>
  NULL
  [2]=>
  NULL
}

->debug()

object(medoo)#6 (9) {
  ["database_type":protected]=>
  string(5) "mysql"
  ["prefix":protected]=>
  NULL
  ["statement":protected]=>
  object(PDOStatement)#11 (1) {
    ["queryString"]=>
    string(307) "UPDATE "clients" SET "name" = :MeDoOmEdOo_10, "accountLimit" = :MeDoOmEdOo_11, "website" = :MeDoOmEdOo_12, "email" = :MeDoOmEdOo_13, "road" = :MeDoOmEdOo_14, "houseNumber" = :MeDoOmEdOo_15, "city" = :MeDoOmEdOo_16, "country" = :MeDoOmEdOo_17, "preferredLanguage" = :MeDoOmEdOo_18 WHERE "id" = :MeDoOmEdOo_19"
  }
  ["option":protected]=>
  array(0) {
  }
  ["logs":protected]=>
  array(1) {
    [0]=>
    array(2) {
      [0]=>
      string(307) "UPDATE "clients" SET "name" = :MeDoOmEdOo_10, "accountLimit" = :MeDoOmEdOo_11, "website" = :MeDoOmEdOo_12, "email" = :MeDoOmEdOo_13, "road" = :MeDoOmEdOo_14, "houseNumber" = :MeDoOmEdOo_15, "city" = :MeDoOmEdOo_16, "country" = :MeDoOmEdOo_17, "preferredLanguage" = :MeDoOmEdOo_18 WHERE "id" = :MeDoOmEdOo_19"
      [1]=>
      array(10) {
        [":MeDoOmEdOo_10"]=>
        array(2) {
          [0]=>
          string(26) "Van der Valk Hotel Beveren"
          [1]=>
          int(2)
        }
        [":MeDoOmEdOo_11"]=>
        array(2) {
          [0]=>
          string(1) "5"
          [1]=>
          int(2)
        }
        [":MeDoOmEdOo_12"]=>
        array(2) {
          [0]=>
          string(26) "http://www.hotelbeveren.be"
          [1]=>
          int(2)
        }
        [":MeDoOmEdOo_13"]=>
        array(2) {
          [0]=>
          string(20) "lal@qms.com"
          [1]=>
          int(2)
        }
        [":MeDoOmEdOo_14"]=>
        array(2) {
          [0]=>
          string(13) "Gentseweg 280"
          [1]=>
          int(2)
        }
        [":MeDoOmEdOo_15"]=>
        array(2) {
          [0]=>
          string(4) "264f"
          [1]=>
          int(2)
        }
        [":MeDoOmEdOo_16"]=>
        array(2) {
          [0]=>
          string(16) "9120 Beverenqsdf"
          [1]=>
          int(2)
        }
        [":MeDoOmEdOo_17"]=>
        array(2) {
          [0]=>
          string(7) "Belgium"
          [1]=>
          int(2)
        }
        [":MeDoOmEdOo_18"]=>
        array(2) {
          [0]=>
          string(1) "1"
          [1]=>
          int(2)
        }
        [":MeDoOmEdOo_19"]=>
        array(2) {
          [0]=>
          string(2) "25"
          [1]=>
          int(2)
        }
      }
    }
  }
  ["logging":protected]=>
  bool(false)
  ["debug_mode":protected]=>
  bool(true)
  ["guid":protected]=>
  int(20)
  ["pdo"]=>
  object(PDO)#7 (0) {
  }
}
ghost commented 7 years ago

the update() always return a PDO statement... that is expected.

if you want to test if the update() is successfull you can use error() depending of what you want to know about the update of course. Per example for my script i always use a wrapper that return me always same output. per example : return [0 => TRUE|FALSE, 1=> $rows]

Where $rows are an array. could have the result or else depending of the query wrapper.

turbopixel commented 7 years ago

Affected rows stored in rowCount():

$medoo->pdo->rowCount();

or try this:

$updateResult = $medoo->update(/* ... */);
$updateResult->rowCount();

source:

nealoke commented 7 years ago

Ooh waaw, I'm not sure how I missed this... Thanks for the input @turbopixel and @superdeeid

catfan commented 7 years ago

It's not a string. It's PDO statement object. You can use its methods for whatever you needed. Like ->rowCount() getting the number row affected, or ->errorInfo() for error info.