I could not solve the illegal string offset error

Hello,
I get this error in the “PHP Warning: Illegal string offset” field in the following code
Can you help me

include "doviz.php";
$doviz = new Doviz();
$kurlar = $doviz->getAll();


$dsatis = number_format($kurlar['USD']['BanknoteSelling'],3,".",".");
$esatis = number_format($kurlar['EUR']['BanknoteSelling'],3,".",".");
$bireuro_dolar = number_format($kurlar['EUR']['CrossRateOther'],3,".",".");


$kurlar = array();
$kurlar = array('id'=>1, 'doviz_cinsi'=>'USD', 'birime'=>'TL', 'tcmb_kur'=>$dsatis);
$kurlar = array('id'=>2, 'doviz_cinsi'=>'EURO', 'birime'=>'TL', 'tcmb_kur'=>$esatis);
$kurlar = array('id'=>3, 'doviz_cinsi'=>'EURO', 'birime'=>'USD', 'tcmb_kur'=>$bireuro_dolar);

       if($dsatis > 0 AND $esatis > 0 AND $bireuro_dolar > 0){
  foreach ($kurlar AS $key => $value){
     $id = $value['id'];                        // PHP Warning:  Illegal string offset 'id' in
     $doviz_cinsi = $value['doviz_cinsi'];      // PHP Warning:  Illegal string offset 'doviz_cinsi' in
     $birime = $value['birime'];                // PHP Warning:  Illegal string offset 'birime' in
     $tcmb_kur = $value['tcmb_kur'];            // PHP Warning:  Illegal string offset 'tcmb_kur' in
     
       $sorgu = "UPDATE dovizkuru SET doviz_cinsi='$doviz_cinsi', birime='$birime', tcmb_kur='$tcmb_kur' WHERE id='$id'";

        if ($mysqli->query($sorgu) === TRUE) {
            $sonuc="yes";
        } else {
            $sonuc="no";
        }

  }

       }else{
       $sonuc="TCMB'DAN KURLAR ALINAMADI";
       }
//}
$mysqli->close();

The key you are using in the array doesn’t exist.

use isset or empty or the coalesce operator to check it.

$kurlar = array();
$kurlar = array('id'=>1, 'doviz_cinsi'=>'USD', 'birime'=>'TL', 'tcmb_kur'=>$dsatis);
$kurlar = array('id'=>2, 'doviz_cinsi'=>'EURO', 'birime'=>'TL', 'tcmb_kur'=>$esatis);
$kurlar = array('id'=>3, 'doviz_cinsi'=>'EURO', 'birime'=>'USD', 'tcmb_kur'=>$bireuro_dolar);

       if($dsatis > 0 AND $esatis > 0 AND $bireuro_dolar > 0){
  foreach ($kurlar AS $key => $value){

You don’t actually see what’s happening here. You are doing key value pairs, so your iteration changes. As a test do this to see what happens;

$kurlar = array();
$kurlar = array('id'=>1, 'doviz_cinsi'=>'USD', 'birime'=>'TL', 'tcmb_kur'=>$dsatis);
$kurlar = array('id'=>2, 'doviz_cinsi'=>'EURO', 'birime'=>'TL', 'tcmb_kur'=>$esatis);
$kurlar = array('id'=>3, 'doviz_cinsi'=>'EURO', 'birime'=>'USD', 'tcmb_kur'=>$bireuro_dolar);

       if($dsatis > 0 AND $esatis > 0 AND $bireuro_dolar > 0){
  foreach ($kurlar AS $key => $value){
       echo "$key : $value <br>";
}

There’s a number of technical problems in the posted code -

  1. Storing formatted numbers in the database table is more work, since you cannot perform math operations on the formatted value. You should only use number_format() when you DISPLAY a number.
  2. By using a dot “.” as both the thousands separator and the decimal point character, the numbers are ambiguous. I suspect you intended to use a comma “,” as the decimal point character?
  3. When you build the $kurlar array in the code, you are only getting the last line, as each assignment statement is overwriting the previous line. To append each line to the array, you would use $kurlar[] = … When you loop over this corrected array, the $key value will just be 0,1,2, which you don’t need. The $value is then each successive array and the elements you are referencing in the code will then exist.

Lastly, how do you know that the ids 1,2, and 3 exist in the dovizkuru table and that they correctly correspond to the doviz_cinsi/birime combinations shown? It’s generally not a good idea to assume that any id value corresponds to a specific row of data. If what you are doing is just updating the conversion rate, you would use the doviz_cinsi and birime values to identify the row to update and in fact, you may want to use an INSERT … ON DUPLICATE KEY UPDATE … query so that if there isn’t already a row, a new row will get inserted.

Thanks for your help,
I’m not a professional, 'm an amateur

This script exchange rate withdrawal, from xml

No new rows, will always be updated

    DROP TABLE IF EXISTS dovizkuru;
    CREATE TABLE `dovizkuru` (
      `id` int(50) NOT NULL AUTO_INCREMENT,
      `doviz_cinsi` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
      `birime` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
      `tcmb_kur` varchar(6) CHARACTER SET utf8 NOT NULL,
      `elle_kur` varchar(6) CHARACTER SET utf8 NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

    --
    -- Tablonun veri dökümü `dovizkuru`
    --

    INSERT INTO `dovizkuru` VALUES('1','USD','TL','5.704','5.747');
    INSERT INTO `dovizkuru` VALUES('2','EURO','TL','6.247','6.275');
    INSERT INTO `dovizkuru` VALUES('3','EURO','USD','1.095','1.092');

Note: The “elle_kur” column field is for manually updating the exchange rate from the admin panel
doviz%20tablo

Sponsor our Newsletter | Privacy Policy | Terms of Service