PHP PDO does not work with every write operation

All SELECTs work correctly. However, any UPDATE or INSERT, even if it works when it is executed in the mysql query browser, connects to the server when logging in with the same user as PHP, and fails when executing PDO.

As an example:

SELECT *  FROM Projects WHERE ssdUserID = :ssdUserID;

works correctly all the time whereas

INSERT INTO Projects SET ssdUserID = :ssdUserID, json = :json;

always executed when PHP is executed, although I add addlashes () for all parameters.

What I call PDO consists of prepared statements:

$connection = new PDO("mysql:host=$mysqlServer;
    dbname=$mysqlSchema", $mysqlUser, $mysqlPassword);;
$statement = $connection->prepare($sql);
$result = $statement->execute($parameters);

$ result is always false, and although a commit occurs at the end (for debugging purposes at the moment), there is no update or insertion in the database.

mysql, , sql mysql - , , - , .

, , PDO . , , , , - , , PDO.

, , , json . PDO , VARCHAR, .

- ? - ?


Update:

(http://www.php.net/manual/en/pdo.lobs.php) . inpput , . , ppl, , , .

. :

01  private function __extractResultsEx($statement, $columns)
02  {
03      $data = array();
04      $msg = "query returned ".$statement->rowCount()." rows";
05      $receiverRow = array();
06      $i = 1;
07      foreach ($columns as $column => $type)
08      {
09          $receiverRow[$column] = NULL;
10          $statement->bindColumn($i++, &$receiverRow[$column], $type);
11      }
12      while ($statement->fetch(PDO::FETCH_BOUND))
13      {
14          $row = array();
15          foreach ($columns as $column => $type)
16              $row[$column] = $receiverRow[$column];
17          $data[] = $row;
18      }
19      return $data;
20  }

: ID/PDO:: PARAM_INT, userID/PDO:: PARAM_INT json/PDO:: PARAM_LOB.

, 09, , $receiverRow , , , , .

, 04 $, , while ( 14-17), $statement-fetch() false, no PDO:: FETCH_ ( PDO:: FETCH_OBJ, PDO:: FETCH_BOUND PDO:: FETCH_ASSOC, , , , PDO:: FETCH_BOUND ).

, , , LOB . , , , , , , .

However, one improvement now. The mySQL trace shows the SQL that I am executing. Also, since line 04 is talking about the line that I am expecting, I suppose I'm close to a solution, but it seems like I can't nail it.
+3
source share
2 answers

You have a few problems:

Make sure it $parametersis an array:

array('ssdUserID' => "val", 'json' => "{}");

In addition, you do not need to call addslasheswhen using prepared statements. PDO does it for you.

To find the error, you can add it (after the call $statement->execute()):

var_dump($connection->errorInfo());

PS: you have too many semicolons per line new PDO(....

+2
source

bindParam execute? text.

$statement->bindParam(':ssdUserID',$user_id, PDO::PARAM_INT);
$statement->bindParam(':json',$json_str,  PDO::PARAM_STR,strlen($json_str) );
$result = $statement->execute();
0

Source: https://habr.com/ru/post/1793426/


All Articles