2

I'm trying to write all iteam names and prices to database but I don't know why is replaceing in database to ★ example ★ Bayonet to ★ Bayonet

I know code maybe is bad becouse I'm beginner

cost.json looks like these:

{
  "status" : "success",
  "prices" : [
    {
      "market_hash_name" : "★ Bayonet",
      "price" : "147.41",
      "created_at" : 1453784357
    },
    {
      "market_hash_name" : "★ Bayonet | Blue Steel (Battle-Scarred)",
      "price" : "112.28",
      "created_at" : 1453784518
    }, ...

Here are my code that I'm using:

<?php
@include_once ("pdocon.php");

$url = 'cost.json';
$api = file_get_contents($url);
$json = json_decode($api, true);


foreach($json['prices'] as $item) {
    $itemname = $item['market_hash_name'];
    $itemcost = $item['price'];

    $stmt = $dbh->prepare("SELECT * FROM items WHERE name=?");
    $stmt->execute(array($itemname));
    $rs = $stmt->fetch(PDO::FETCH_ASSOC);
    if(!empty($rs)) {
        if(time()-$rs["lastupdate"] < 604800) die($rs["cost"]);
    }

    $stmt = $dbh->prepare("UPDATE items SET `cost` = ?,`lastupdate` = ? WHERE `name` = ?");
    $stmt->execute(array($itemcost, time(), $itemname));

    $stmt = $dbh->prepare("INSERT INTO items (`name`,`cost`,`lastupdate`) VALUES (?, ?, ?)");
    $stmt->execute(array($itemname, $itemcost, time()));


}

?>
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
R3ason
  • 86
  • 1
  • 2
  • 8

1 Answers1

1

Try to set your PDO connection options and pass it, when connecting to your database:

$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
$handle = new PDO(dns, user, pass, $options);
Andreas Schrammel
  • 463
  • 1
  • 6
  • 11