PDO not working when updating database

Hello everyone, I’ve spent 2 full days trying to find out why the code below is not updating my MySql database…

The code is working locally but once I upload it, it’s executing without any error or warning (FYI, I have this feature activated on InfinityFree).

Honestly I don’t understand, and the same code is working in case of INSERT into database.

Thank you in advance for your help.

My username : epiz_25903923

The PHP code below is simplified but this is where there is a problem. For information :

  • word_i is string
  • score_i is a number
  • history_i is string

and all of this is matching with the database and all word_i exist already in the database with values in columns score and history.

$ToAdd=[[word_1, score_1, history_1], ... ,[word_n, score_n, history_n]];

$connect = new PDO('mysql:host=sql101.epizy.com;dbname=DBNAME','USERNAME','PASSWORD');

$query = "
UPDATE table_name 
SET column1=:column1, column2=:column2
WHERE column3=:column3;
";

  $statement = $connect->prepare($query);

  for($i = 0; $i < count($ToAdd); $i++)
  {
   $statement->execute(
    array(
      ':column1'   => $ToAdd[$i][0],
      ':column2'  => $ToAdd[$i][1],
      ':column3'  => $ToAdd[$i][2]
      )
   );
  }
  $result = $statement->fetchAll();

Thank you all who will read this and have a great day.

First of all, are you using a MySQLi Database? If yes, why the hell are you using PDO?

This technically is an invalid MySQL Statement. Ive never worked with PDO so I don’t know if there is something im missing.

If you were executing this statement using MySQLi it would look something like this:

$query = "UPDATE table_name 
SET column1='value2', column2='value2'
WHERE column3='value2';

If you were doing it as Prepared statements:

$query = "UPDATE table_name 
SET column1 = ?, column2 = ?
WHERE column3 = ?;

Also generally I find that trying to set more than 1 value doesn't work. Ive had this issue ;-;

I don’t see any code that shows error message if it fails to execute, I don’t have 3rd eye to guess what the error is about.

3 Likes

Just FYI: the database software is called MySQL. MySQLi is a PHP module you can use to connect to a MySQL database server. PDO is another PHP module, which provides a generic interface to interact with databases, which also supports MySQL.

MySQLi and PDO are both valid ways to connect to a MySQL database. It’s personal preference which one you choose to use.


As for the issue itself, I’m very curious as to whether the query runs successfully or not. The $statement->execute() function should return either a TRUE or FALSE value, depending on whether the query was successful.

Does the query return successfully or not?

If it doesn’t, you can enable display_errors in the Alter PHP Config menu in your control panel to show the error messages being generated from the query.

4 Likes

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.